mvc Areas注册域常见问题一
添加Areas主要目的是区分一些不同的业务,避免不同的业务都在同一个Controllers下造成混乱,在MVC项目上右键->添加区域->我添加了HMbolie和PClient两个区域->如下图
![](https://images2015.cnblogs.com/blog/230041/201601/230041-20160127094517223-1401190909.png)
namespace Demo.Areas.HMbolie
{
public class HMbolieAreaRegistration : AreaRegistration
{ public override string AreaName { get { return"HMbolie";
} } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "HMbolie_default", "HMbolie/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new string[] { "Demo.Areas.HMbolie.Controllers" } ); } }
} namespace Demo.Areas.PClient { public class PClientAreaRegistration : AreaRegistration { public override string AreaName { get { return "PClient"; } }
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "PClient_default", "PClient/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new string[] { "Demo.Areas.PClient.Controllers" } ); } } }
此时Areas已经创建完成,但是在iis下运行的时候感觉Areas没有起到作用,在官网中看到Areas是通过Global中的Application_Start()中的 AreaRegistration.RegisterAllAreas();来完成注册的,代码如下:
/// <summary> /// ApplicationStart /// </summary> protected void Application_Start() { //System.Threading.Thread.Sleep(10000); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
我在Start()中线程睡10s中才启动,断点调试发现AreaRegistration.RegisterAllAreas()中根本没有找到HMbolieAreaRegistration.cs和PClientAreaRegistration.cs这两个相关的Areas类,iis下的代码是最新部署,但是却不执行?
解决方法如下:删除iis缓存文件
- C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files
- 我删除了Temporary ASP.NET Files 下的缓存文件后,重启网站然后调试代码,一切都如愿的加载了。