MVC5面积不工作 [英] MVC5 Area not working
问题描述
我的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.Collections.Generic;
using System.Linq;
使用System.Web;
using System.Web.Mvc;
using System.Web.Routing;
命名空间BlocqueStore_Web
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes。 IgnoreRoute({resource} .axd / {* pathInfo});
routes.MapRoute(
name:Default,
url:{controller} / {action} / {id},
默认值: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文件 p>
使用System.Web.Mvc;
命名空间BlocqueStore_Web.Areas.Admin
{
public class AdminAreaRegistration:AreaRegistration
{
public override string AreaName
{
get
{
returnAdmin;
}
}
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}
);
}
}
}
c> 区。在默认值
Admin ,并对索引
> 参数 c> context.MapRoute
方法
public override void RegisterArea(AreaRegistrationContext上下文)
{
context.MapRoute(
Admin_default,
Admin / {controller} / {action} / {id},
默认值:new {action =Index,controller =Admin,id = UrlParameter.Optional},
namespaces:new [] {BlocqueStore_Web.Areas.Admin.Controllers}
);
}
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?
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" }
);
}
这篇关于MVC5面积不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!