我有一个MVC 4 Web应用程序,其中包含一些区域。我对名为“Catalog”的区域的路由规则有疑问。 RouteConfig.cs文件是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
}
和Global.asax如下:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
CatalogAreaRegistration就像这样:
public class CatalogAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Catalog";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Catalog_default",
"Catalog/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
问题是当我调试时,RouteCollection路由不包括在该区域中定义的规则。我使用routedebugger并看到路由集合不包含“目录”区域的规则。它只有RouteConfig中的规则。
我不知道是什么问题。提前致谢。
答案 0 :(得分:19)
我认为由于Visual Studio的缓存,一些dll没有正确编译,这种情况可能会发生。如果这样做,请从以下位置删除所有临时文件:
更新:
然后重新启动Visual Studio。这就是我的决心。
答案 1 :(得分:1)
只需将控制器的命名空间添加到AreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
**namespaces: new string[] { "Web.Admin.Controllers" }**
);
}