MVC MapRoute 未使用所需参数进行路由
我希望我的 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 代码,但提供的参数什么也没有。
例如 函数详细信息(ByVal id As String) As ActionResult 调暗 viewModel 作为新的 XViewModel(id) 返回视图(viewModel) End Function
id 的值被设置为空,而不是预期的 Bob。
有什么想法吗?
I'm wanting the URL to my MVC application to be like:
www.site.com/Bob
Which would in turn 'redirect' to Home/Details/Bob.
I've set up the following MapRoute:
routes.MapRoute( _
Nothing, _
"{personName}", _
New With {.controller = "Home", .action = "Details", .personName = ""}, _
New With {.result = New NameConstraint()} _
)
The NameConstraint is an IRoutingConstraint that checks to see the name exists. If it does it return true, else returns false which causes it to the go to the second (and default) 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
The problem arises when I call: www.site.com/Bob. It correctly routes to the Home/Details code in the controller, but the supplied parameter is nothing.
E.g.
Function Details(ByVal id As String) As ActionResult
Dim viewModel As New XViewModel(id)
Return View(viewModel)
End Function
The value of id is set to nothing, and not Bob as expected.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
在你的地图路线中
PS:幸运的是我的名字不是 Home ...
Try this:
in your maproute
PS: Luckily my name is not Home ...