MVC中Area的另一种用法
【摘要】本文只是为一行代码而分享
context.MapRoute("API", "api/{controller}/{action}", new { }, newstring[] { "CNBlogs.UcHome.Web.Controllers.Api" });
我们在ASP.NET MVC中使用Area时通常这么干:
在Web项目中创建Areas文件夹,在其中创建对应的Area文件夹,在其下创建Controllers文件夹。然后在Area文件夹中创建AreaRegistration的子类用于注册Area路由,在Controllers文件夹中创建所需的Controller。

这么干有个前提,就是你的Web项目类型要是WebApplication。
而我们所处的场景是:Web项目类型是WebSite。之前在使用MVC时,将所有的Controller放在了一个单独的类库项目中。

我们今天有一个需求,需要用area来解决。为什么要用area?举个例子来说明。
比如我们在页面中添加网摘时,访问的网址是 home.cnblogs.com/wz/add ,而供其他应用调用网摘API的网址是 home.cnblogs.com/api/wz/add 。这里的wz对应的控制器名称都是WzController,Action都是add,实际也的确存在两个WzController,放在不同的文件夹中。如上图,一个在项目根文件夹,一个在Api文件夹。所以在网址中通过api前缀路径来区分,在程序中也要让ASP.NET路由找到对应的Controller。
这时Area就发挥作用了,但由于Conroller不在Web项目中,所以要找其他方法解决这个问题。方法来自Migrating a large web application to ASP.NET MVC(关键在最后一行代码):
When AreaRegistration.RegisterAllAreas() is called, ASP.NET will search for all AreaRegistration subclasses in all assemblies in the bin dir so that we do not have to modify the global.asax when we add new areas.
In the AreaRegistration subclass, we would do something like:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Services",
"Sub1/Sub11/Sub111/{controller}.mvc/{action}/{id}",
new { action = "Index", id = "" },
new string[] { "My.Custom.Namespace.Controllers" }
);
}
AreaRegistration.RegisterAllAreas()是global.asax中调用的,它会找到所有的AreaRegistration的子类,不管是在Web项目中,还是在其他类库项目中。所以我们在CNBlogs.UcHome.Web.Controllers项目的Api文件夹中放一个AreaRegistration的子类,也是能被找到的,然后在注册Area时,在参数中传递Controller所在的命名空间,问题就解决了。

【更新】
虽然通过ApiArea解决了api的路由问题,但是这时我们访问非api的路径(比如/wz/my),出现下面的错误:
Multiple types were found that match the controller named 'wz'. This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'wz' has found the following matching controllers:
CNBlogs.UcHome.Web.Controllers.Api.WzController
CNBlogs.UcHome.Web.Controllers.WzController
也就是说,如果有两个同名的Controller,必须在路由时指定命名空间,routes.MapRoute的第5个参数就是用于指定命名空间,我开始时不知道有这个参数,于是试图用Area来解决问题,实际是一个错误的解决方法,正确的解决方法是在Global.asax中针对不同的命名添加两个不同的路由,示例代码如下:

routes.MapRoute("API",
"api/{controller}/{action}",
new { },
null,
new string[] { "CNBlogs.UcHome.Web.Controllers.Api" }
);
routes.MapRoute("DefaultMvc",
"{controller}/{action}/{id}",
new { id = UrlParameter.Optional },
null,
new string[] { "CNBlogs.UcHome.Web.Controllers" }
);

MVC中Area的另一种用法的更多相关文章
- ASP.NET MVC中Area的另一种用法
ASP.NET MVC中Area的另一种用法 [摘要]本文只是为一行代码而分享 context.MapRoute("API", "api/{controller}/{ac ...
- mysql中模糊查询的四种用法介绍
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- Spring MVC 中获取session的几种方法
Spring MVC 中使用session是一种常见的操作,但是大家上网搜索一下可以看到获取session的方式方法五花八门,最近,自己终结了一下,将获取session的方法记录下来,以便大家共同学习 ...
- Spring MVC中forward请求转发2种方式(带参数)
Spring MVC中forward请求转发2种方式(带参数) http://www.51gjie.com/javaweb/956.html
- 下面介绍mysql中模糊查询的四种用法:
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- MVC中Area的使用
1.Area是什么? MVC 2 中引进了区域的概念,它允许将模型,视图和控制器分成单独的功能节点,换句话说,可以在大型复杂的网站中建立几个区域(模块),每一个区域都有Model,View,Contr ...
- JSP中的include的两种用法
1.两种用法 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 2.用法区别 (1)执行时间上区别 < ...
- java中 this 关键字的三种用法
Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...
- python 中 dict 的另一种用法
python 中 dic另一种用法 描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 dict={} dict.get(k1,Non ...
随机推荐
- 微信小程序-微信自动退款(Java后台)
微信小程序-微信自动退款 1.首先分享 微信自动退款接口: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 微信付款 代码案例 ...
- Python的高级特性(切片,迭代,生成器,迭代器)
掌握了python的数据类型,语句和函数,基本上就可以编出很多有用的程序了. 但是在python中,并不是代码越多越好,代码不是越复杂越好,而是越简单越好. 基于这个思想,就引申出python的一些高 ...
- 138.括号序列(区间型DP)
3657 括号序列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 我们用以下规则定义一个合法的括号序列: ...
- 8VC Venture Cup 2016 - Final Round D. Preorder Test 二分 树形dp
Preorder Test 题目连接: http://www.codeforces.com/contest/627/problem/D Description For his computer sci ...
- sql server 2012 登录失败出现10048错误
好久没用sql server了,今天登录我的一个数据库中的时候突然登录不了,账号密码没有错,到服务中看的时候看到我那个数据库没有启动,心想启动应该就好了. 然后出现了这个错误,度娘了一下,网上大家的方 ...
- MathType中带上下标字符不对其
如图,上面的好看,下面的就不好看的. 上面的图使用下图下面的形式,下面的图是用的是上面的形式. 如图可以看出,右侧的更好. 比如UiTVj这样的,需要分别都用下面的形式,不能UiT用上面的,Vj直接输 ...
- GridControl事件
private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventA ...
- [Android Pro] ListView,GridView之LayoutAnimation特殊动画的实现
转载自:http://gundumw100.iteye.com/blog/1874545 LayoutAnimation干嘛用的?不知道的话网上搜一下. Android的Animation之Layou ...
- linux下使用C++ Json库
安装Json库 1.下载JsonCpphttp://sourceforge.net/projects/jsoncpp/files/ 2.下载sconshttp://sourceforge.net/pr ...
- 【算法导论C++代码】最大子数组
#define Inf 65535 #include <iostream> using namespace std; void FindMaxCrossingSubarray(int *A ...