ASP.NET MVC 源码更新预览
原文请看:ScottGu的原文ASP.NET MVC Source Refresh Preview
这里只是概括说一下。
注:A few hours ago we published a refresh of the ASP.NET MVC source code on the site. This source refresh is not an official new ASP.NET MVC preview release - instead it is an interim drop that provides a look at the current state of the source tree. (直接帖原文了,就是说这不是一个官方发布的预览版,只是暂时的,是对MVC Preview 3目前状况的一个预览)
MapRoute()方法是重载的并可以有两个、三个、四个参数(route name, URL syntax, URL parameter default, and URL parameter regular expression constraints).正则,哈哈,这个帅。
我们然后就可以参考这个"Products-Browse"规则来在我们的Controller和Action来为它产生URL。例如,我们可以用Html.RouteLink view helper来产生一个URL:
产生的URL如下所示:
Note: with this week's source drop you need to pass-in the controller and action parameters (in addition to the Category param) to the Html.RouteLink() helper to resolve the correct route URL to generate. The ASP.NET MVC Preview 3 drop in a few weeks will not require this, and allow you to use the Html.RouteLink call exactly as I've written it above to resolve the route.
Other URL Route Mapping Features
This week's MVC source drop also supports a bunch of new URL route mapping features. You can now include "-", ".", ";" or any other characters you want as part of your route rules.
For example, using a "-" separator you can now parse the language and locale values from your URLs separately using a rule like below:
This would pass appropriate "language", "locale", and "category" parameters to the ProductsController.Browse action method when invoked:
URL Route Rule | Example URL | Parameters Passed to Action method |
{language}-{locale}/products/browse/{category} | /en-us/products/browse/food | language=en, locale=us, category=food |
/en-uk/products/browse/food | language=en, locale=uk, category=food |
Or you can use the "." file extension type at the end of a URL to determine whether to render back the result in either a XML or HTML format:
This would pass both "category" and a "format" parameters to the ProductsController.Browse action method when invoked:
URL Route Rule | Example URL | Parameters Passed to Action method |
products/browse/{category}.{format} | /products/browse/food.xml | category=food, format=xml |
/products/browse/food.html | category=food, format=html |
ASP.NET MVC Preview 2 introduced wildcard route rules. For example, you can indicate in a rule to pass all remaining URI content on as a named parameter to an action method:
This would pass a "contentUrl" parameter to the WikiController.DisplayPage action method when invoked:
URL Route Rule | Example URL | Parameters Passed to Action method |
Wiki/Pages/{*contentUrl} | /Wiki/Pages/People/Scott | contentUrl="People/Scott" |
/Wiki/Pages/Countries/UK | contentUrl="Countries/UK" |
These wildcard routes continue to work fine with this week's preview - and are very useful to look at if you are building a blogging, wiki, cms or other content based system.
Note that in addition to using the new routing system for ASP.NET MVC scenarios, we are also now using the same routing system within ASP.NET Dynamic Data (which uses ASP.NET Web Forms).
对于URL Routing中的修改,可以看下微软的MVC主要负责人之一的Phil Haack先生的文章:
Routing中的一些修改 - You've been HAACKED中文版 - 博客堂
http://blog.joycode.com/haacked/archive/2008/04/14/115071.aspx
睡觉,后面的将就着看,不想看就等博客堂的中文版. -。-
发表评论
:会员注册