【问题标题】:ASP.NET Core 3.1 areas return 404 on some methods but not on the Index oneASP.NET Core 3.1 区域在某些方法上返回 404,但在索引一上没有
【发布时间】:2020-03-28 03:35:59
【问题描述】:

我有以下简单的解决方案,其中一个名为 Test 的区域:

在我的 Startup.cs 中:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)
            .AddAzureADB2C(options => Configuration.Bind("AzureAdB2C", options));
        services.AddControllersWithViews();
        services.AddRazorPages();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "test",
                pattern: "Test/{controller=Map}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
    }
}

在 MapController.cs 中:

[Area("Test")]
[Route("test/[controller]")]
public class MapController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [Route("[action]")]
    public IActionResult LoadNearbySitesAsync()
    {
        return Ok("data");
    }
}

当我尝试联系https://localhost:44319/Test/Map/Index 时,会显示索引页面。 当我尝试访问 https://localhost:44319/Test/Map/LoadNearbySitesAsync 时,我收到了 HTTP 404 异常:

当我尝试使用 jQuery $.get 函数访问 LoadNearbySitesAsync 方法时,我也收到了 HTTP 404 异常。

之前,我使用的是 ASP.NET Core 2.2,它运行良好。现在我切换到 ASP.NET Core 3.1 和新的 Endpoints 东西,我无法让它工作。

我尝试了[Area] 和[Route] 属性的不同组合,我什至在LoadNearbySitesAsync 方法上添加了[Route("[action]")] 属性,但到目前为止没有任何效果。

知道我在这里缺少什么吗?

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:
    1. 从 LoadNearbySitesAsync 操作中删除 [Route("[action]")] 并从控制器中删除 [Route("Test/[controller]")]
    2. 将MapControllerRoute 更改为MapAreaControllerRoute
    app.UseEndpoints(endpoints => {
            endpoints.MapControllers();
            endpoints.MapAreaControllerRoute(
                "Test",
                "Test",
                "Test/{controller=Map}/{action=Index}/{id?}");
    
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");  
    });
    
    1. 将动作名称从LoadNearbySitesAsync更改为 LoadNearbySites 或拨打此网址 https://localhost:44319/Test/Map/LoadNearbySites

    有关更多信息,您可以查看link

    【讨论】:

    • 我遵循了您的建议,但仍然得到相同的结果 (404)。感谢您的链接,内容丰富!
    • @DotNetMatt 很高兴我能帮助你。但我更新了我的答案,它成功了
    • 谢谢@Farhad!!它奏效了,我认为缺少的关键元素是对endpoints.MapControllers() 的调用。
    • @DotNetMatt 祝你好运:)
    • 对我来说,它是向控制器添加区域属性 [Area("Test")],这使我的解决方案。添加端点.MapControllers();行似乎没有什么不同。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2020-07-10
    相关资源
    最近更新 更多