我的 MVC 5 应用程序中有两个区域无法正常工作。
当我使用以下链接 http://localhost:45970/Admin/Admin
时,应用会加载正确的 index.cshtml whicxh 位于 /Areas/Admin/Views/Admin/Index.cshtml
但是,当我尝试加载 http://localhost:45970/Admin
时,它会尝试从 /Views/Admin/Index 加载 Index.cshtml 文件。 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" }
);
}
}
}
这是我的 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);
}
最后是我的 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" }
);
}
}
}
那么,我错过了什么?
最佳答案
注册 Admin
区域时没有设置默认 Controller 。在 context.MapRoute
方法的 defaults
参数中将 Controller 设置为 Admin
并将操作设置为 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" }
);
}
关于c# - MVC5 区域不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987453/