Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过路由重定向到更具体的URL_C#_Asp.net Mvc 3_Url Routing - Fatal编程技术网

C# 通过路由重定向到更具体的URL

C# 通过路由重定向到更具体的URL,c#,asp.net-mvc-3,url-routing,C#,Asp.net Mvc 3,Url Routing,我在我的项目中使用以下路由 routes.MapRoute( "ProductRoute", // Route name "product/{id}/{title}", // URL with parameters new { controller = "product", action = "Index", id = UrlParameter.Optional }, // Parameters defaults new[] { "PriceCompare.Con

我在我的项目中使用以下路由

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一样。例如,如果您访问以下URL

stackoverflow.com/questions/734249/,您的浏览器地址将在浏览器中显示完整的URLstackoverflow.com/questions/734249/asp net mvc URL路由以及多个路由值


如何实现这一点?

您可以实施自己的
路线
或在行动中执行类似操作:

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
}

您应该在操作中评估
title
参数。如果它是空的,您应该从数据库中获取标题并用标题重定向到url。当有人访问特定url时,您需要执行301重定向,这就是StackOverflow的工作方式。你如何选择重定向可能是个问题。你可以通过一个动作过滤器来实现这一点