MVC 添加Area
在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰。
步骤如下:
项目 –> 添加 -> 区域 ( Area )
输入 Admin
添加成功后
Area包含:
创建一个空MVC工程结构类似,Admin Area 有自己的 Controllers、Models 和
Views 文件夹,不一样的地方就是多了一个 AdminAreaRegistration.cs
文件,这个文件中定义了一个叫 AdminAreaRegistration 的类,它的内容如下:

根目录可以放一套一样的结构用来做前端开发使用,而admin 目录一般会作为管理员后台来开发!
AdminAreaRegistration.cs 文件,这个文件中定义了一个叫 AdminAreaRegistration 的类,它的内容如下:
namespace MvcApp4.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 { controller = "home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" } //指定该路由查找控制器类的命名空间
);
}
}
}
在这里需要注意需加入 Areas 所在的命名空间,来控制 controllers 接收的参数,不然访问会出现错误,往下一点会提到。
namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }
AreaRegistrationContext 类的 MapRoute 方法和 App_Start-> RouteConfig.cs 的 MapRoute 方法的使用是一样的,只是区分Area 目录下的路由控制!
在 Global.asax 中的 Application_Start 方法会自动加了这样一句代码
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
调用 AreaRegistration.RegisterAllAreas 方法让MVC应用程序在启动后会寻找所有继承自 AreaRegistration 的类,并为每个这样的类调用它们的 RegisterArea 方法。
下面我们来做一个 Demo
新建两个访问连接,内容都是一样,都是简单输出一个 "hello World"
URL定位到 (areas/admin)
http://localhost:18291/Admin/Home/Index
URL定位到(根目录)
http://localhost:18291/Home/Index
public class HomeController : Controller
{
//
// GET: /Admin/Home/ public ActionResult Index()
{
return Content("hello world");
} }
如果刚才没有加入:
namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }
运行后就会出现如下错误:

但是如果我们把根目录下的 /Home/Index 的内容输出改成 “Root Say hello World” , 你会发现还是输出 “ hello World ”,
这是就是 Controller的歧义问题
这就是我们需要注意的另一个地方
我们需要在App_start下的 RouteConfig.cs 也要增加一个 namespaces 来声明 Controller 访问的命名空间!
//App_start下的 RouteConfig.cs
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[] { "MvcApp4.Controllers" }//指定该路由查找控制器类的命名空间 controllers
);
}
}
//areas 下的 \Admin\AdminAreaRegistration.cs
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 { controller = "home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" } //对应的命名空间的 controllers
);
}
}
这样访问时就可以区分 , 不同目录的 controller
MVC 添加Area的更多相关文章
- MVC添加Area出现“到多个与名为“Home”的控制器匹配的类型的解决方法”
新建MVC项目,添加HomeController,然后添加名字为Admin的Area后,新建HomeController.这个时候,运行项目会出现以下错误: 解决办法如下: 打开网址下面的HomeCo ...
- ASP.NET MVC中Area的另一种用法
ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...
- MVC 插件化框架支持原生MVC的Area和路由特性
.NET MVC 插件化框架支持原生MVC的Area和路由特性 前面开放的源码只是简单的Plugin的实现,支持了插件的热插拔,最近晚上偶然想到,原生的MVC提供Areas和RouteAtrribut ...
- MVC中Area的使用
1.Area是什么? MVC 2 中引进了区域的概念,它允许将模型,视图和控制器分成单独的功能节点,换句话说,可以在大型复杂的网站中建立几个区域(模块),每一个区域都有Model,View,Contr ...
- ASP.NET MVC 创建 Area 以及使用
此博客全乘抄袭,只为以后自己能再次用到 参考链接 http://www.cnblogs.com/willick/p/3331519.html ASP.NET MVC允许使用 Area(区域)来组织We ...
- ASP.NET没有魔法——ASP.NET MVC使用Area开发一个管理模块
之前的My Blog提供了列表的展示和文章显示功能,但是所有数据仍然只能通过数据库录入,为了完成最初的角色“作者”的用例,本章将介绍如何使用“Area”实现My Blog的管理功能. 根据功能分离代码 ...
- MVC中Area的另一种用法
[摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{action}", new { }, n ...
- Asp.net MVC 基于Area的路由映射
对于一个较大规模的Web应用,我们可以从功能上通过Area将其划分为较小的单元.每个Area相当于一个独立的子系统,具有一套包含Models.Views和Controller在内的目录结构和配置文件. ...
- mvc添加多条数据到数据库
from : http://www.th7.cn/Program/net/201502/387849.shtml mvc的视图太强大了,个人刚刚接触.(初级菜鸟,懂的不多,往大神们指点)需求是,客户点 ...
随机推荐
- iOS 打包 测试 发布
1.企业版 1.1 打包 1.1.1 使用apple企业账号 获取 证书cer,描述文件provision (开发 生产) *注: 描述文件 又 三者组成(cer + appId + bundleId ...
- python学习笔记(一)
• Python能干嘛?[1]科学计算[2]图形化开发[3]系统脚本[4]Web服务器[5]网络爬虫[6]服务器集群自动化运维 • 常用工具:easy_install.pip.ipython.Subl ...
- js parsefloat
项目中需要对返回的小数进行格式化,把零省略掉. 1.00 ---> 1 1.01 ---> 1.01 1.10 ---> 1.1 parseFloat() 函数可解析一个 ...
- 安装jhipster
基础软件安装 安装JDK,需要配置环境变量.暂时使用1.8版本 安装maven,需要配置环境变量. http://maven.apache.org/ 安装Node.js ,https://nodej ...
- Python PIP安装
https://zhidao.baidu.com/question/550936793.html 按图做
- 总结:JSP几种提交表单方法
问题描述: 最近进了一家“老公司”工作,说他老不是说他成立的早,是因为他的编程框架太l.......low了.EJB的规范模式,使用是IBM经过Eclipse二次开发出来的RAD(Rational A ...
- URL-Short
Fortify URL http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html 1.Arstechnica http://a ...
- 没有素描色彩基础适合学习UI吗,如果可以,该怎么学?
文章背景,来自[ UI设计交流群 - 92588284 ]的一次探讨,由于个人视野有限,个中观点有失狭隘,仅供参考,欢迎拍砖: 问题一:一个朋友想学UI设计,没有任何基础的,也没有美术功底,想问问 ...
- KALI Linux problems & Study Red Hat | Ubuntu
Problem When you ask some website with https head.you may met the problem secure connection failed ...
- JavaScript基础(一)之语法、变量、数据类型
1.JavaScript语法 ①区分大小写 ②弱类型变量 ③每行结尾分号可有可无 ④括号用于代码块 ⑤注释有两种方式(单行和多行注释) 2.JavaScrip变量 ①用Var声明,不要初始化 ②可以在 ...