C# ASP.NET Core 3.1区域在某些方法上返回404,但在索引方法上不返回404
我有以下简单的解决方案,其中一个区域名为Test: 在my Startup.cs中:C# ASP.NET Core 3.1区域在某些方法上返回404,但在索引方法上不返回404,c#,asp.net-core,C#,Asp.net Core,我有以下简单的解决方案,其中一个区域名为Test: 在my Startup.cs中: public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called b
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");
}
}
当我尝试访问时,索引页显示出来。
当我尝试访问时,会出现HTTP 404异常:
当我尝试使用jQuery$.get
函数访问LoadNearbySitesAsync
方法时,我还收到一个HTTP 404异常
以前,我使用的是ASP.NETCore2.2,它运行良好。现在我切换到ASP.NETCore3.1和新的端点之类的东西,我无法让它工作
我尝试了不同的属性组合[Area]
和[Route]
,甚至在LoadNearbySitesAsync
方法中添加了[Route([action]”)属性,目前为止没有任何效果
你知道我在这里遗漏了什么吗
从LoadNearbySitesAsync
action中删除[路由(“[action]”)
,并从控制器中删除[路由(“[Test/[controller]”)]
将MapControllerRoute
更改为MapAreaControllerRoute
app.UseEndpoints(端点=>{
endpoints.MapControllers();
endpoints.MapAreaControllerRoute(
“测试”,
“测试”,
“Test/{controller=Map}/{action=Index}/{id?}”);
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Home}/{action=Index}/{id?}”);
});
将操作名称从LoadNearbySitesAsync
更改为
LoadNearbySites
或调用此url
https://localhost:44319/Test/Map/LoadNearbySites
有关更多信息,请查看此项
从LoadNearbySitesAsync
action中删除[路由(“[action]”)
,并从控制器中删除[路由(“[Test/[controller]”)]
将MapControllerRoute
更改为MapAreaControllerRoute
app.UseEndpoints(端点=>{
endpoints.MapControllers();
endpoints.MapAreaControllerRoute(
“测试”,
“测试”,
“Test/{controller=Map}/{action=Index}/{id?}”);
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Home}/{action=Index}/{id?}”);
});
将操作名称从LoadNearbySitesAsync
更改为
LoadNearbySites
或调用此url
https://localhost:44319/Test/Map/LoadNearbySites
有关更多信息,您可以查看此项我遵循了您的建议,但仍然得到了相同的结果(404)。谢谢你的链接,它的信息@我很高兴能帮助你。但是我更新了我的答案,它成功地运行了Tanks@Farhad!!它起作用了,我认为缺少的关键元素是调用endpoints.MapControllers()
@DotNetMatt Good luck:)对我来说,是向控制器添加了区域属性[area(“Test”)],这使我的解析得以实现。添加endpoints.MapControllers();这句话似乎没什么不同。我按照你的建议做了,但还是得到了同样的结果(404)。谢谢你的链接,它的信息@我很高兴能帮助你。但是我更新了我的答案,它成功地运行了Tanks@Farhad!!它起作用了,我认为缺少的关键元素是调用endpoints.MapControllers()
@DotNetMatt Good luck:)对我来说,是向控制器添加了区域属性[area(“Test”)],这使我的解析得以实现。添加endpoints.MapControllers();这句话似乎没什么不同。