MVC MapRoute 未使用所需参数进行路由

发布于 2024-10-15 01:27:29 字数 1400 浏览 5 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2024-10-22 01:27:29

试试这个:

 New With {.controller = "Home", _
.action = "Details", _
.personName = UrlParameter.Optional}, 

在你的地图路线中

PS:幸运的是我的名字不是 Home ...

Try this:

 New With {.controller = "Home", _
.action = "Details", _
.personName = UrlParameter.Optional}, 

in your maproute

PS: Luckily my name is not Home ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文