Joh*_*ltz 4 c# asp.net asp.net-mvc asp.net-mvc-areas asp.net-mvc-5
我的MVC 5应用程序中有两个区域无法正常工作。
当我使用以下链接时http://localhost:45970/Admin/Admin,应用会加载正确的index.cshtml whicxh,/Areas/Admin/Views/Admin/Index.cshtml但是当我尝试加载时,http://localhost:45970/Admin它会尝试从加载Index.cshtml文件/Views/Admin/Index.cshtml。
所有搜索结果都表明我在做正确的事。我什至已经加载了一个示例API项目,以查看其中的帮助区域,以确保我做的正确。
这是我的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" }
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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);
}
Run Code Online (Sandbox Code Playgroud)
最后是我的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" }
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么,我想念什么?
注册Admin区域时未设置默认控制器。在方法的参数中将控制器设置为Admin并将动作设置为Indexdefaultscontext.MapRoute
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" }
);
}
Run Code Online (Sandbox Code Playgroud)