MVC5 Area not working

核能气质少年 提交于 2019-12-10 17:13:21

问题


I have two Areas in my MVC 5 app that are not working properly.

When I use the following Link http://localhost:45970/Admin/Admin the app loads the proper index.cshtml whicxh is located at /Areas/Admin/Views/Admin/Index.cshtml however when I try to load http://localhost:45970/Admin it tries to load the Index.cshtml file from /Views/Admin/Index.cshtml.

All the search results say I am doing the correct thing. I have even loaded a sample API project to look at the help area in it to make sure I was doing things correctly.

Here is my RouteConfig.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace BlocqueStore_Web
{
    public class RouteConfig
    {
        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 },
                namespaces: new[] { "BlocqueStore_Web.Controllers" }
            );
        }
    }
}

Here is the Application_Start() section of my Global.asax.cs file

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

And finally my AdminAreaRegistration.cs file

using System.Web.Mvc;

namespace BlocqueStore_Web.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration 
    {
       public override string AreaName 
       {
          get 
          {
             return "Admin";
          }
       }

       public override void RegisterArea(AreaRegistrationContext context) 
       {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "BlocqueStore_Web.Areas.Admin.Controllers" }
            );
        }
    }
}

So, what am I missing?


回答1:


You didn't set the default controller when registering Admin area. Set the controller to Admin and action to Index in the defaults parameter of context.MapRoute method

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        defaults: new { action = "Index", controller = "Admin", id = UrlParameter.Optional },
        namespaces: new[] { "BlocqueStore_Web.Areas.Admin.Controllers" }
    );
}


来源:https://stackoverflow.com/questions/38987453/mvc5-area-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!