在使用area 的时候,设置默认的显示页面,经常会碰到如下异常:
“/”应用程序中的服务器错误。
未找到视图“Index”或其母版视图,或没有视图引擎支持搜索的位置。搜索了以下位置:
~/Views/index/Index.aspx
~/Views/index/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/index/Index.cshtml
~/Views/index/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
但直接输入url地址却可以访问。这时就是Global.asax文件的配置有问题了。具体配置如下:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "index", action = "Index", id = UrlParameter.Optional }, // 参数默认值
new string[] { "Index.Controllers" } //默认控制器的命名空间
).DataTokens.Add("area", "index"); //默认area 的控制器名称
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
文件 IndexAreaRegistration.cs 的配置如下:
public class IndexAreaRegistration : AreaRegistration { public override string AreaName { get { //项目名称 如与区域名称不一致,则需要更改生成事件的批处理 return "Index"; } } public override void RegisterArea(AreaRegistrationContext context) { //路由规则小写 context.MapRoute( "index_default", "index/{controller}/{action}/{id}", new { controller = "indexhome", action = "index", id = UrlParameter.Optional } ); } }