ASP.NET Core 3.1区域在某些方法上返回404,但不在索引1上返回404 [英] ASP.NET Core 3.1 areas return 404 on some methods but not on the Index one
问题描述
我有以下简单的解决方案,其中一个区域名为Test:
在我的Startup.cs中:
公共类启动{公共启动(IConfiguration配置){配置=配置;}公共IConfiguration配置{}//此方法由运行时调用.使用此方法将服务添加到容器.公共无效ConfigureServices(IServiceCollection服务){services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme).AddAzureADB2C(options => Configuration.Bind("AzureAdB2C",options));services.AddControllersWithViews();services.AddRazorPages();}//此方法由运行时调用.使用此方法来配置HTTP请求管道.公共无效配置(IApplicationBuilder应用程序,IWebHostEnvironment env){如果(env.IsDevelopment()){app.UseDeveloperExceptionPage();}别的{app.UseExceptionHandler("/Home/Error");//HSTS的默认值为30天.您可能要针对生产方案更改此设置,请参见https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();app.UseAuthentication();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(名称:"test",模式:测试/{controller = Map}/{action = Index}/{id?}");endpoints.MapControllerRoute(名称:默认",模式:"{controller = Home}/{action = Index}/{id?}");endpoints.MapRazorPages();});}}
在MapController.cs中:
[Area("Test")][Route("test/[controller]")]公共类MapController:控制器{公共IActionResult Index(){返回View();}[Route("[action]")]公共IActionResult LoadNearbySitesAsync(){返回Ok("data");}}
当我尝试访问
当我尝试使用jQuery $.get
函数访问 LoadNearbySitesAsync
方法时,我也收到HTTP 404异常.
之前,我使用的是ASP.NET Core 2.2,并且工作正常.现在,我切换到ASP.NET Core 3.1和新的Endpoints东西,我无法使它正常工作.
我尝试了属性 [Area]
和 [Route]
的不同组合,甚至添加了 [Route("[action]")]
LoadNearbySitesAsync
方法上的code>属性,到目前为止没有任何效果.
知道我在这里缺少什么吗?
- 从
LoadNearbySitesAsync
操作中删除[Route("[action]")]
并删除[Route("Test/[controller]")] 代码>来自控制器
- 将
MapControllerRoute
更改为MapAreaControllerRoute
app.UseEndpoints(endpoints => {endpoints.MapControllers();endpoints.MapAreaControllerRoute(测试",测试","Test/{controller = Map}/{action = Index}/{id?}");endpoints.MapControllerRoute(名称:默认",模式:"{controller = Home}/{action = Index}/{id?}");});
- 将操作名称从
LoadNearbySitesAsync
更改为LoadNearbySites
或调用此网址https://localhost:44319/Test/Map/LoadNearbySites
有关更多信息,您可以检查此链接
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?
- Remove
[Route("[action]")]
fromLoadNearbySitesAsync
action and remove[Route("Test/[controller]")]
from controller - Change the
MapControllerRoute
toMapAreaControllerRoute
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?}");
});
- change the name of action from
LoadNearbySitesAsync
toLoadNearbySites
or call this urlhttps://localhost:44319/Test/Map/LoadNearbySites
for more information you can check this link
这篇关于ASP.NET Core 3.1区域在某些方法上返回404,但不在索引1上返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!