简体   繁体   English

MVC5区域无法正常工作

[英]MVC5 Area not working

I have two Areas in my MVC 5 app that are not working properly. 我的MVC 5应用程序中有两个区域无法正常工作。

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 . 当我使用以下链接http://localhost:45970/Admin/Admin ,应用程序加载正确的index.cshtml whicxh位于/Areas/Admin/Views/Admin/Index.cshtml但是当我尝试加载http://localhost:45970/Admin它尝试从/Views/Admin/Index.cshtml加载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. 我甚至加载了一个示例API项目来查看其中的帮助区域,以确保我正确地执行操作。

Here is my RouteConfig.cs file 这是我的RouteConfig.cs文件

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 这是我的Global.asax.cs文件的Application_Start()部分

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 最后是我的AdminAreaRegistration.cs文件

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? 那么,我错过了什么?

You didn't set the default controller when registering Admin area. 注册Admin区域时未设置默认控制器。 Set the controller to Admin and action to Index in the defaults parameter of context.MapRoute method context.MapRoute方法的defaults参数中将控制器设置为Admin ,将action设置为Index

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" }
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM