您的位置:首页 > 编程语言 > ASP

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了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: