簡體   English   中英

ASP.NET Core 3.1 區域在某些方法上返回 404,但在索引一上不返回

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

我有以下簡單的解決方案,其中一個名為 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 時,會顯示 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]")]屬性,但LoadNearbySitesAsync沒有任何效果。

知道我在這里缺少什么嗎?

  1. LoadNearbySitesAsync操作中刪除[Route("[action]")]LoadNearbySitesAsync刪除[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. 將操作名稱從LoadNearbySitesAsyncLoadNearbySites或調用此 URL https://localhost:44319/Test/Map/LoadNearbySites

有關更多信息,您可以查看此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM