我也一直在寻找如何做到这一点,只是理解了它。
这里是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