【问题标题】:MVC Rewriting URLMVC 重写 URL
【发布时间】:2016-03-28 11:27:35
【问题描述】:

我有一个为某些产品编写动态 URL 的 Ajax 调用。

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

结果链接是:

Frutta_e_Verdura/SchedaProdotto?idProdotto=83&nome=anacardi

我想用 routeconfig 的形式转它:

Frutta_e_Verdura/SchedaProdotto/anacardi

我正在尝试使用此路由配置:

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 } );

我该怎么做?

感谢大家的光临

【问题讨论】:

    标签: c# model-view-controller url-rewriting asp.net-mvc-routing


    【解决方案1】:

    路由是将传入的 URL 映射到资源,而不是更改浏览器中的 URL。唯一的方法是使用 302 或 301 重定向。

    但是,由于重定向 URL 会导致您的服务器向客户端发送响应,指示它向服务器发出另一个请求,因此这是不必要的网络往返。

    您最好的选择是从您的 AJAX 调用中正确写入 URL 以匹配您定义的 prodotti 路由以避免这种不必要的重定向。

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

    【讨论】:

    • 很抱歉,但我认为这与您所说的不完全一样。我有这个 url website.com/Frutta_e_Verdura/…,我必须重写 URL 以使其对 seo 友好。我该怎么做?
    猜你喜欢
    • 2011-04-23
    • 2012-06-03
    • 2015-07-15
    • 2015-07-24
    • 2012-07-14
    • 2011-08-24
    • 1970-01-01
    • 2013-01-19
    • 2023-03-05
    相关资源
    最近更新 更多