1
所以我有ASP.NET MVC應用程序。我想配置它的路線。以下是我的RouteConfig代碼:路由約束
public static void Register(RouteCollection routes, bool useAttributes = true)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
此路線工作正常。此外,我在我的應用程序中有一個區域,並嘗試配置它的路由。這是我區的註冊代碼:
public override void RegisterArea(AreaRegistrationContext context)
{
try
{
context.MapRoute(
name: "SiteSettings_Controller",
url: "SiteSettings/{controller}/{action}/{id}",
defaults: new {action = "Index", id = UrlParameter.Optional,
// here I tried to use @"(UserManagement|Tools|Settings)"
//as constraint but it takes no effect
constraints: new {controller = "UserManagement|Tools|Settings" }
);
}
catch (Exception e)
{
// here I get InvalidOperationException ""
}
}
我想限制SiteSettingsArea的路線控制器,但是當我去爲「localhost /站點設置/ UserManagement」網址我得到消息InvalidOperationException異常「在路由表中沒有路由匹配提供的值「。我相信這個網址對應SiteSettings_Controller路線,但顯然我錯了。我怎樣才能正確限制路線中的控制器?