Я тоже искал, как это сделать, и только что понял.
Вот код в 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
Вы заметите, что есть два префиксных маршрута: admin / controller / action и manager / controller / action. Вы можете добавить столько, сколько хотите.
- Edit--
Первоначально это работало, пока я не попытался использовать 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