0

MVC アプリケーションへの URL を次のようにしたい:

www.site.com/ボブ

これにより、ホーム/詳細/ボブに「リダイレクト」されます。

次の MapRoute をセットアップしました。

    routes.MapRoute( _
Nothing, _
"{personName}", _
New With {.controller = "Home", .action = "Details", .personName = ""}, _
New With {.result = New NameConstraint()} _
)

NameConstraint は、名前の存在を確認する IRoutingConstraint です。一致する場合は true を返し、それ以外の場合は false を返し、2 番目の (デフォルトの) 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 に電話すると問題が発生します。コントローラーのホーム/詳細コードに正しくルーティングされますが、指定されたパラメーターは何もありません。

例 Function Details(ByVal id As String) As ActionResult Dim viewModel As New XViewModel(id) Return View(viewModel) End Function

id の値は何も設定されておらず、予想どおり Bob ではありません。

何か案は?

4

1 に答える 1

1

これを試して:

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

あなたのmaprouteで

PS:幸い私の名前はホームではありません...

于 2011-01-26T22:08:32.650 に答える