首页 / C# / c# – MVC5区域无法正常工作
c# – MVC5区域无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – MVC5区域无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2147字,纯文字阅读大概需要4分钟。
内容图文
![c# – MVC5区域无法正常工作](/upload/InfoBanner/zyjiaocheng/796/6965c86f0d8a4803b7c2c6e646f9223c.jpg)
我的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# – MVC5区域无法正常工作全部内容,希望文章能够帮你解决c# – MVC5区域无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。