MVC路由 - 改变航线 [英] MVC Routing - changing routes
本文介绍了MVC路由 - 改变航线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用MVC4,我有以下路由的一篇博客文章中详细的行动是搜索引擎友好URLS:
Using MVC4, I have the following routing for a blog post detail action which is SEO friendly URLS:
public ActionResult Details(int id, string postName)
{
BlogPost blogPost = _blogService.GetBlogPostById(id);
string expectedName = blogPost.Title.ToSeoUrl();
string actualName = (postName ?? "").ToLower();
if (expectedName != actualName)
return RedirectToAction("Details", "Blog", new { id = blogPost.BlogPostId, postName = expectedName });
var vm = BuildBlogPostDetailViewModel(id);
return View(vm);
}
使用以下助手方法构建搜索引擎优化路线:
The SEO route is constructed using the following helper method:
public static class Helper
{
public static string ToSeoUrl(this string url)
{
// ensure the the is url lowercase
string encodedUrl = (url ?? "").ToLower();
// replace & with and
encodedUrl = Regex.Replace(encodedUrl, @"\&+", "and");
// remove characters
encodedUrl = encodedUrl.Replace("'", "");
// remove invalid characters
encodedUrl = Regex.Replace(encodedUrl, @"[^a-z0-9]", "-");
// remove duplicates
encodedUrl = Regex.Replace(encodedUrl, @"-+", "-");
// trim leading & trailing characters
encodedUrl = encodedUrl.Trim('-');
return encodedUrl;
}
}
这产生了路线,像这样:
This produces a route like so:
/博客/详细信息/ 1?postName =用户组 - 2013
/Blog/Details/1?postName=user-group-2013
我想实现如下路线:
/博客/详细信息/用户组 - 2013
/Blog/Details/user-group-2013
这是如何实现和优化这个有什么建议?
Any suggestions on how to achieve and optimize this?
非常感谢
推荐答案
试试这个
return RedirectToAction("Details", "Blog", new { blogPost.BlogPostId,expectedName });
这篇关于MVC路由 - 改变航线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文