有没有办法从控制器更改当前的url参数,所以当加载页面时,地址栏中会显示其他/不同的参数?
这就是我的意思,说我有一个行动'产品':
public ActionResult Product(int productId)
{
..
}
我映射了路由以便product/4545/purple-sunglasses
映射到上面的函数,实际上忽略了产品名称,但是我想,如果没有指定产品名称,控制器应该添加它,所以产品很容易进入搜索引擎等。
答案 0 :(得分:7)
看看这里:http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls
有很长的描述如何做到这一点。最后一部分将向您介绍301重定向,您应该使用它来指示搜索引擎抓取工具在您希望的URL下找到该页面。
不要忘记查看网址编码,应该为您节省一些工作并提供更高质量的网址。
以下是博客文章中的一些重要摘录:
设置路由:
routes.MapRoute(
"ViewProduct",
"products/{id}/{productName}",
new { controller = "Product", action = "Detail", id = "", productName = "" }
);
将名称部分添加到控制器并检查它是否是正确的名称:
public ActionResult Detail(int id, string productName)
{
Product product = IProductRepository.Fetch(id);
string realTitle = product.Title; // Add encoding here
if (realTitle != urlTitle)
{
Response.Status = "301 Moved Permanently";
Response.StatusCode = 301;
Response.AddHeader("Location", "/Products/" + product.Id + "/" + realTitle); // Or use the UrlHelper here
Response.End();
}
return View(product);
}
<强>更新强>
网址显然已被打破。本文主要介绍相同的功能:http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx
感谢Stu1986C的评论/新链接!