Asp.net mvc 4 MVC路由-更改路由
使用MVC4,我有以下博客文章细节操作的路由,这是SEO友好的URL:Asp.net mvc 4 MVC路由-更改路由,asp.net-mvc-4,routes,asp.net-mvc-routing,Asp.net Mvc 4,Routes,Asp.net Mvc Routing,使用MVC4,我有以下博客文章细节操作的路由,这是SEO友好的URL: public ActionResult Details(int id, string postName) { BlogPost blogPost = _blogService.GetBlogPostById(id); string expectedName = blogPost.Title.ToSeoUrl(); string actualName = (postName ?? "").ToLower
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);
}
SEO路线使用以下辅助方法构建:
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;
}
}
这将生成如下所示的路线:
/Blog/Details/1?postName=user-group-2013
我正在努力实现以下目标:
/博客/详细信息/用户组-2013
关于如何实现和优化这一点,有什么建议吗
非常感谢试试这个
return RedirectToAction("Details", "Blog", new { blogPost.BlogPostId,expectedName });
试试这个
return RedirectToAction("Details", "Blog", new { blogPost.BlogPostId,expectedName });
您可以尝试在RouteConfig类中更改路由 似乎您只有默认路线:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// This route will return /Blog/Details/1/user-group-2013
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
// Or this route. It should return /Blog/Details/user-group-2013
routes.MapRoute(
name: "MyRoute2",
url: "{controller}/{action}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
在默认路由之前还有一条路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// This route will return /Blog/Details/1/user-group-2013
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
// Or this route. It should return /Blog/Details/user-group-2013
routes.MapRoute(
name: "MyRoute2",
url: "{controller}/{action}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
您可以尝试在RouteConfig类中更改路由 似乎您只有默认路线:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// This route will return /Blog/Details/1/user-group-2013
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
// Or this route. It should return /Blog/Details/user-group-2013
routes.MapRoute(
name: "MyRoute2",
url: "{controller}/{action}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
在默认路由之前还有一条路由:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// This route will return /Blog/Details/1/user-group-2013
routes.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);
// Or this route. It should return /Blog/Details/user-group-2013
routes.MapRoute(
name: "MyRoute2",
url: "{controller}/{action}/{postName}",
defaults: new { controller = "Blog", action = "Details", id = UrlParameter.Optional, postName = UrlParameter.Optional}
);