ASP.NET MVC实践系列1-UrlRouting
2009-10-26 11:12
441 查看
为了调研ASP.NET MVC是否适合在公司项目中应用,研究了一段时间。感觉网上资料中讲实践的比较少,我在这里总结一下以备以后查用。
ASP.NET MVC 包含了一个强大的URL路由引擎,它允许我们自定义选择使用哪个控制器类,根据不同的参数来控制调用哪个action方法。ASP.NET MVC 中有一套默认的规则来简化控制类以及action方法的调用,如果不了解这个默认规则,在使用中比较容易让人迷惑,我们结合ASP.NET MVC模板来简单了解一下这些默认规则。当我们根据ASP.NET MVC的模板创建一个ASP.NET MVC应用时,我们可以在Global.asax文件中找到以下代码:
Code
public void Page_Load(object sender, System.EventArgs e)
{
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
仔细阅读这段代码你会发现,其实它的作用就是将地址转换到根上,也就是将http//localhost:4804/default.aspx转换成http//localhost:4804/,那么路由就可以根据url来选择相应的Controller和action了。
ASP.NET MVC 包含了一个强大的URL路由引擎,它允许我们自定义选择使用哪个控制器类,根据不同的参数来控制调用哪个action方法。ASP.NET MVC 中有一套默认的规则来简化控制类以及action方法的调用,如果不了解这个默认规则,在使用中比较容易让人迷惑,我们结合ASP.NET MVC模板来简单了解一下这些默认规则。当我们根据ASP.NET MVC的模板创建一个ASP.NET MVC应用时,我们可以在Global.asax文件中找到以下代码:
Code
public void Page_Load(object sender, System.EventArgs e)
{
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
仔细阅读这段代码你会发现,其实它的作用就是将地址转换到根上,也就是将http//localhost:4804/default.aspx转换成http//localhost:4804/,那么路由就可以根据url来选择相应的Controller和action了。
相关文章推荐
- ASP.NET MVC实践系列-UrlRouting
- ASP.NET MVC实践系列11-FCKEditor和CKEditor的使用
- ASP.NET MVC实践系列2-简单应用
- ASP.NET MVC实践系列8-对查询后分页处理的解决方案
- ASP.NET MVC实践系列10-单元测试
- ASP.NET MVC实践系列12-表单处理(转)
- ASP.NET MVC实践系列6-Grid实现(上)
- ASP.NET Web API实践系列06, 在ASP.NET MVC 4 基础上增加使用ASP.NET WEB API
- ASP.NET MVC 实践系列4-Ajax应用
- ASP.NET MVC实践系列12-表单处理
- ASP.NET MVC实践系列7-Grid实现(下-利用Contrib实现)
- Asp.Net MVC实践 - 探索UrlRouting并分析UrlHelper (基于ASP.NET MVC Preview 3)
- ASP.NET MVC系列:UrlRouting
- ASP.NET MVC实践系列5-结合jQuery
- ASP.NET MVC实践系列9-filter原理与实践
- ASP.NET MVC实践系列3-服务器端数据验证
- ASP.NET MVC实践系列4-Ajax应用
- ASP.NET MVC系列:UrlRouting
- MSDN Webcast 系列课程之《ASP.NET MVC 3 开发实践系列课程》
- Asp.Net MVC实践 - 探索UrlRouting并分析UrlHelper (基于ASP.NET MVC Preview 3) 【转】