Ich habe auch die Suche, wie dies zu tun und dachte, es einfach aus. Hier
ist, den Code in VB (es sollte nicht schwer zu C# zu übersetzen)
Erstellen Sie eine Klasse Umsetzung 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
Als nächstes fügen Sie diese Ihr 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
Sie werden feststellen, dass es zwei Präfix Routen: admin/controller/action und Manager/controller/action. Sie können beliebig viele hinzufügen.
--Edit-- Zunächst schien das zu funktionieren, bis ich versuchte, mit Html.Actionlink Links zu erstellen. Dies veranlasst ActionLink, admin/an jede generierte URL anzuhängen. Also habe ich es korrigiert. Dies sollte bewirken, dass das Präfix-Routing ähnlich wie das Admin-Routing von CakePHP funktioniert.
hier ist die edit:
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
Es gibt nicht genug Informationen, um zu wissen, ob dies eine gute Antwort ist. Ich frage speziell, wie man Präfix-Routing in ASP.NET MVC macht. Wie hilft mir der Blick auf die Basisklasse? –
Ahh ok, das sieht nach dem aus was ich suche. Ich werde es später später versuchen und zu dir zurückkommen. –
um den Bereich zu behandeln, setzen Sie einfach den DataTokens ["Bereich"]. requestContext.RouteData.DataTokens ["Bereich"] = requestContext.RouteData.Values ["yourCustomAreaToken"] – kite