我在我的 ASP.NET Core 3.1 应用程序 (MVC) 中使用了区域。
现在我希望所有没有明确区域的请求默认转到“主要”区域。这是我目前设置端点路由的方式:
app.UseEndpoints(endpoints =>
{
// 1
endpoints.MapControllerRoute(
name: "area",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
// 2
endpoints.MapAreaControllerRoute(
name: "default",
areaName: "Main",
pattern: "{area=Main}/{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
我的目标是:
如果请求 URL 包含现有区域名称,请使用路由 [1]。如果没有区域名称,则使用路由[2](默认为“Main”区域)。
我的问题:
如何设置默认区域?
好的,解决了。最后,这对我有用:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute(
name: "default",
areaName: "Main",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud) 来自www:
...路由引擎将采用与提供的URL匹配的第一个路由,并尝试使用该路由中的路由值.因此,应首先在表中添加不太常见或更专业的路由,而稍后应添加更一般的路由......
我为什么要先绘制专业路线?有人可以举个例子,我可以看到"地图常见路线优先"的失败吗?
asp.net-mvc url-routing asp.net-mvc-routing asp.net-mvc-5 asp.net-mvc-5.2