繁体   English   中英

通过路由重定向到更特定的URL

[英]Redirect to more specific URL via routing

我在项目中使用以下路由

routes.MapRoute(
    "ProductRoute", // Route name
    "product/{id}/{title}", // URL with parameters
    new { controller = "product", action = "Index", id = UrlParameter.Optional }, // Parameters defaults
    new[] { "PriceCompare.Controllers" }
    );

当前的问题是如何显示URL。 可以通过以下任何一种方式访问​​URL:

一切都很好,因为所有这些URL都重定向到所需的位置。 但是,即使有人使用第二种或第三种方法,我认为也不错,浏览器中的返回URL应该显示第一个URI。

就像StackOverflow。 例如,如果您访问以下网址

stackoverflow.com/questions/734249/ ,您的浏览器地址将在浏览器中显示完整的URL stackoverflow.com/questions/734249/asp-net-mvc-url-routing-with-multiple-route-values

如何做到这一点?

您可以实现自己的Route或在操作中执行以下操作:

public ActionResult Index(int? id, string title = null)
{
    if (String.IsNullOrWhiteSpace(title))
    {
         var product = // load product
         return Redirect(Url.Action("Index", "Product",
                                    new { id = id, title = product.Title }));
    }

    // your code
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM