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:
如何实现这一点?您可以实施自己的
路线
或在行动中执行类似操作:
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的工作方式。你如何选择重定向可能是个问题。你可以通过一个动作过滤器来实现这一点