我有一个 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 中的规则。
我不知道出了什么问题。提前致谢。
最佳答案
我认为由于 Visual Studio 的缓存,某些 dll 未正确编译,因此可能会发生这种情况。如果这样做,请从以下位置删除所有临时文件:
- C:\Temp
- C:\Users\%用户名%\AppData\Local\Microsoft\VisualStudio
- C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\临时 ASP.NET 文件
- 路径\到\您的\项目\obj\调试
更新:
- AppData\Local\Temp\临时 ASP.NET 文件
然后重新启动 Visual Studio。我就是这样解决的。
关于asp.net-mvc - AreaRegistration.RegisterAllAreas() 不是区域注册规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18313571/