简体   繁体   中英

ASP.NET Core 3.1 areas return 404 on some methods but not on the Index one

I have the following simple solution, with one Area named Test:

解决方案

In my 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();
        });
    }
}

And in the MapController.cs:

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

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

When I try to reach https://localhost:44319/Test/Map/Index , the Index page is showing up. When I try to reach https://localhost:44319/Test/Map/LoadNearbySitesAsync , I get an HTTP 404 exception:

在此处输入图片说明

I also get an HTTP 404 exception when I try accessing the LoadNearbySitesAsync method with jQuery $.get function.

Before, I was using ASP.NET Core 2.2 and it was working fine. Now that I switched to ASP.NET Core 3.1 and the new Endpoints stuff, I can't get this to work.

I tried different combinations of attributes [Area] and [Route] , I even added a [Route("[action]")] attribute on the LoadNearbySitesAsync method, nothing worked so far.

Any idea what I am missing here?

  1. Remove [Route("[action]")] from LoadNearbySitesAsync action and remove [Route("Test/[controller]")] from controller
  2. Change the MapControllerRoute to 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. change the name of action from LoadNearbySitesAsync to LoadNearbySites or call this url https://localhost:44319/Test/Map/LoadNearbySites

for more information you can check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM