我希望我的MVC应用程序的URL如下:
www.site.com/Bob
反过来“重定向”到Home / Details / Bob。
我已经设置了以下MapRoute:
routes.MapRoute( _
Nothing, _
"{personName}", _
New With {.controller = "Home", .action = "Details", .personName = ""}, _
New With {.result = New NameConstraint()} _
)
NameConstraint是一个IRoutingConstraint,用于检查名称是否存在。如果是,则返回true,否则返回false,导致转到第二个(默认)MapRoute。
Public Class ListingConstraint
Implements IRouteConstraint
Public Function Match(ByVal httpContext As System.Web.HttpContextBase, ByVal route As System.Web.Routing.Route, ByVal parameterName As String, ByVal values As System.Web.Routing.RouteValueDictionary, ByVal routeDirection As System.Web.Routing.RouteDirection) As Boolean Implements System.Web.Routing.IRouteConstraint.Match
Dim personName As String
personName = CStr(values("personName"))
If listingName = "Bob" Then 'akin to checking the database for a valid name
Return True
Else
Return False
End If
End Function
End Class
当我致电:www.site.com/Bob时出现问题。它正确地路由到控制器中的Home / Details代码,但提供的参数是什么。
E.g。 函数详细信息(ByVal id As String)作为ActionResult Dim viewModel As New XViewModel(id) 返回视图(viewModel) 结束功能
id的值设置为nothing,而不是Bob的预期值。
有什么想法吗?
答案 0 :(得分:1)
试试这个:
New With {.controller = "Home", _
.action = "Details", _
.personName = UrlParameter.Optional},
你的maproute中的PS:幸运的是我的名字不是Home ...