我也一直在尋找如何做到這一點,只是理解了它。
這裏是VB代碼(它不應該是很難翻譯成C#)
創建一個類實現IRouteHandler:
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace Util.Routing
''' <summary>
''' This Route Handler looks for a 'prefix' part in the url. If found, it appends the prefix to the action name.
''' Example: /prefix/controller/action
''' Turns into: /controller/prefix_action
''' </summary>
''' <remarks></remarks>
Public Class PrefixRouteHandler
Implements IRouteHandler
Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
Dim prefix = requestContext.RouteData.Values("prefix")
If (prefix IsNot Nothing) Then
' If prefix actually exists in the beginning of the URL
If (requestContext.HttpContext.Request.Path.ToLower().StartsWith("/" & prefix.ToString.ToLower() & "/")) Then
Dim action = requestContext.RouteData.Values("action")
If action Is Nothing Then
action = "index"
End If
requestContext.RouteData.Values("action") = prefix & "_" & action
End If
End If
Return New MvcHandler(requestContext)
End Function
End Class
End Namespace
接下來,這增加你的global.asax。VB:
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("{*favicon}", New With {.favicon = "(.*/)?favicon.ico(/.*)?"})
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
routes.Add(New Route("admin/{controller}/{action}/{id}", _
New RouteValueDictionary(New With {.prefix = "Admin", .controller = "Home", .action = "Index", .id = ""}), _
New PrefixRouteHandler() _
) _
)
routes.Add(New Route("manager/{controller}/{action}/{id}", _
New RouteValueDictionary(New With {.prefix = "Manager", .controller = "Home", .action = "Index", .id = ""}), _
New PrefixRouteHandler() _
) _
)
routes.MapRoute("Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = ""} _
)
End Sub
你會發現,有兩個前綴的路由:管理/控制/動作,以及管理器/控制器/動作。你可以添加儘可能多的,你想要的。
- 編輯 - 最初似乎工作,直到我嘗試使用Html.Actionlink創建鏈接。這會導致ActionLink將admin /添加到每個生成的url。所以,我糾正了它。這應該會導致前綴路由的工作方式與CakePHP的管理路由類似。
這裏是編輯:
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("{*favicon}", New With {.favicon = "(.*/)?favicon.ico(/.*)?"})
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}")
routes.Add("AdminPrefix", _
New Route("{prefix}/{controller}/{action}/{id}", _
New RouteValueDictionary(New With {.controller = "Home", .action = "Index", .id = "", .prefix = ""}), _
New RouteValueDictionary(New With {.prefix = "admin"}), _
New PrefixRouteHandler()))
routes.Add("ManagerPrefix", _
New Route("{prefix}/{controller}/{action}/{id}", _
New RouteValueDictionary(New With {.controller = "Home", .action = "Index", .id = "", .prefix = ""}), _
New RouteValueDictionary(New With {.prefix = "manager"}), _
New PrefixRouteHandler()))
routes.MapRoute("Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = ""} _
)
End Sub
沒有足夠的信息知道這是否是一個很好的答案。我特別要求如何在ASP.NET MVC中做前綴路由。如何看待基類幫助我? – 2009-08-24 20:07:43
啊,好吧,這看起來像我之後。我會在今天晚些時候嘗試並回復你。 – 2009-08-26 21:02:40
來處理區域只需設置DataTokens [「區域」]。 requestContext.RouteData.DataTokens [ 「區域」] = requestContext.RouteData.Values [ 「yourCustomAreaToken」] – kite 2013-04-11 10:17:45