0

I've an Ajax call that write a dynamic Url for some product.

$("#Scheda").html( '<a href="/Frutta_e_Verdura/SchedaProdotto?idProdotto=' + result.Idprodotto + '&nome=' + result.Title + '" target = "_blank" >Scopri di più sul prodotto >>></a>' );

The result link is :

Frutta_e_Verdura/SchedaProdotto?idProdotto=83&nome=anacardi

I want turn it with routeconfig in the form:

Frutta_e_Verdura/SchedaProdotto/anacardi

I am trying with this routeconfig:

routes.MapRoute( name: "prodotti", url: "frutta_e_verdura/schedaprodotto/{nome}/{idprodotto}", defaults: new { controller = "frutta_e_verdura", action = "Index", nome = UrlParameter.Optional ,idprodotto = UrlParameter.Optional } );

How can i do this?

Thank you all for your availability

1 Answer 1

1

Routing is for mapping an incoming URL to a resource, not to change the URL in the browser. The only way to do that is to use a 302 or 301 redirect.

But, as redirecting the URL is going to cause your server to send a response to the client instructing it to do another request to the server, this is an unnecessary round trip across the network.

Your best option is just to write the URL correctly from your AJAX call to match the prodotti route you have defined to avoid this unnecessary redirect.

$("#Scheda").html( '<a href="/Frutta_e_Verdura/SchedaProdotto/' + result.Title + '/' + 
    result.Idprodotto + '" target = "_blank" >Scopri di più sul prodotto >>></a>' );
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry but I think that it isn't exactly as you say. I have this url website.com/Frutta_e_Verdura/… and I have to make a rewriting of the URL to make it seo friendly. How can i do this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.