Asp.net MVC area文件夹下设置默认显示页面

在使用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 }
            );
        }
    }



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值