所以我构建了一个新的ASP.NET MVC4应用程序,一些非常简单的东西。我添加了一个区域用于管理目的。当我在Visual Studio的IIS Express上调试时,一切正常。
当我在Windows Server 2012的IIS上部署此应用程序时出现问题,大多数时候,当我尝试访问我的区域时,我收到404错误。在重新部署应用程序(但不改变任何东西)之后,有时会遇到这种情况,该区域将工作一段时间(直到应用程序池可能被回收?)
无论如何,这真的很受欢迎,我需要亲自动手。这就是我所做的:
这是基础应用程序的RouteConfig
:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "NewsFilter",
url: "Articles/{tagName}",
defaults: new { controller = "Home", action = "Index", tagName = UrlParameter.Optional },
namespaces: new[] { "myapp.Controllers" }
);
routes.MapRoute(
"ViewArticle",
"Article/{articleId}/{articleTitle}",
new { controller = "Article", action = "Index", articleTitle = UrlParameter.Optional },
new { articleId = @"\d+" },
namespaces: new[] { "myapp.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "myapp.Controllers" }
);
}
}
现在这是管理区域的AdminAreaRegistration
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Admin_default",
url: "Admin/Home/{action}/{id}",
defaults: new { controller="Home", action = "Index", id = UrlParameter.Optional },
namespaces: new []{ "myapp.Areas.Admin.Controllers" }
);
context.MapRoute(
name: "Admin_news",
url: "Admin/News/{action}/{id}",
defaults: new { controller="News", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "myapp.Areas.Admin.Controllers" }
);
context.MapRoute(
name: "Admin_Tags",
url: "Admin/Tags/{action}/{id}",
defaults: new { controller = "Tags", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "myapp.Areas.Admin.Controllers" }
);
}
}
现在,请在Application_Start
Global.asax
方法
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); //This registers the admin Area
WebApiConfig.Register(GlobalConfiguration.Configuration); //I did not delete WebApiConfig even though I'm not using it
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes); //And here are my "normal" routes
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
我的管理区域中的所有控制器都在命名空间myapp.Areas.Admin.Controllers
中。主app控制器位于命名空间myapp.Controllers
。
在IIS上,应用程序的应用程序池在.net 4.0集成模式下运行。
知道为什么该区域在调试模式下运行,在IIS上运行有时并在大多数情况下返回404 ?