locked
trouble with url rewriting RRS feed

  • Question

  • User1961461639 posted

    Hi,

    I have a little trouble with url rewriting so I turn to you after a few days of searching without success ^ ^

    Rather than a large blabla I go directly to the example will be clearer.
    If I put the redirect directly in my page it works fine, the page is rewritten no worries

    Mapage.aspx

    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="<%$RouteUrl:RouteName=RechercheParMarqueAuto,i=Audi%>">HyperLink</asp:HyperLink>

    Résultat :

    www.toto.com/Marque/Audi

    ---------------------------------------------------------------------------------------------------------

    But the real problem comes when I go through the code behind, the url isn't rewritten

    Mapage.aspx

    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#" />

    Mapage.aspx.vb

    strNomMarque = "Audi"
    strUrlPageMarques = "<%$RouteUrl:RouteName=RechercheParMarqueAuto,i=" & strNomMarque & "%>"
    HyperLink1.NavigateUrl = strUrlPageMarques
    HyperLink1.ToolTip = strNomMarque
    HyperLink1.Text = strNomMarque

    Résultat :

    www.toto.com/<%$RouteUrl:RouteName=RechercheParMarqueAuto,i=Audi%>

    Why ? Cry

    ---------------------------------------------------------------------------------------------------------

    If it helps this is what I have in the Global.asax

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
    End Sub
    Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.MapPageRoute("RechercheParMarqueAuto", "Marque/{i}", "~/recherche-par-marque-auto.aspx")
    End Sub




    Thank you for helping out Wink

    Wednesday, April 3, 2013 5:17 AM

All replies

  • User1508394307 posted

    Try

    Dim parameters As RouteValueDictionary
    parameters = New RouteValueDictionary(New With _
             {.i = "Audi"})
    
    Dim vpd As VirtualPathData
    vpd = RouteTable.Routes.GetVirtualPath(Nothing, "RechercheParMarqueAuto", parameters)
    HyperLink1.NavigateUrl = vpd.VirtualPath

    http://msdn.microsoft.com/en-us/library/cc668176(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4

    http://forums.asp.net/t/1684203.aspx/1
    http://blog.hmobius.com/post/2010/03/14/ASPNET-40-Part-12-Routing-Helper-Functions.aspx

    Hope this helps.

    Wednesday, April 3, 2013 6:00 AM
  • User1961461639 posted

    It's ok, thanks !!! Smile

    However, I get an error vpd.VirtualPatch with Response.Redirect

    strNomMarque = "Audi"
    
    Dim parameters As RouteValueDictionary
    parameters = New RouteValueDictionary(New With {.i = strNomMarque })
    Dim vpd As VirtualPathData
    vpd = RouteTable.Routes.GetVirtualPath(Nothing, "RechercheParMarqueAuto", parameters)
    
    Response.Redirect(vpd.VirtualPath)

    Error :

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    (Détails de l'exception: System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet. )

    ----------------------------------------------------------------------------------------------
    and if I use

    Response.RedirectToRoute("RechercheParMarqueAuto", New RouteValueDictionary(New With {.i = strNomMarque }))
    
    

    Or

    Response.RedirectToRoute("RechercheParMarqueAuto", New With {.i = strNomMarque})

    Error :

    Exception Details: System.InvalidOperationException: No matching route found RedirectToRoute.
    
    (Détails de l'exception: System.InvalidOperationException: Aucun itinéraire correspondant trouvé pour RedirectToRoute.)

    Thank for your help

    Thursday, April 4, 2013 10:10 AM