c# – MVC5区域无法正常工作
作者:互联网
我的MVC 5应用程序中有两个区域无法正常工作.
当我使用以下链接http:// localhost:45970 / Admin / Admin时,应用程序加载正确的index.cshtml whicxh位于/Areas/Admin/Views/Admin/Index.cshtml但是当我尝试加载http:/时/ localhost:45970 / Admin它尝试从/Views/Admin/Index.cshtml加载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" }
);
}
}
}
这是我的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" }
);
}
}
}
那么,我错过了什么?
解决方法:
注册管理区域时未设置默认控制器.在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" }
);
}
标签:c,asp-net,asp-net-mvc,asp-net-mvc-5,asp-net-mvc-areas 来源: https://codeday.me/bug/20190623/1267926.html