我正在尝试关注此页面http://www.deliveron.com/blog/post/SEO-Friendly-Routes-with-ASPnet-MVC.aspx这就是我在 global.asax 中所拥有的,
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"MemberRoute", // routeName
"member/{userId}/{pseudoName}", // url
new
{ // url defaults
controller = "Member",
action = "Index",
userId = 0,
pseudoName = UrlParameter.Optional
},
new
{ // url constraints
userId = @"\d+" // must match url {userId}
}
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
成员控制器,
public ActionResult Index(int id, string pseudoName)
{
User user;
var unitOfWork = new UnitOfWork();
user = unitOfWork.UserRepository.GetById(id);
var expectedName = user.PseudoName.ToSeoUrl();
var actualName = (pseudoName ?? "").ToLower();
// permanently redirect to the correct URL
if (expectedName != actualName)
return RedirectToActionPermanent("Index", "Member", new { id = user.UserId, pseudoName = expectedName });
return View(user);
}
这是从 AccountController 的登录操作返回的,
return RedirectToAction("Index", "Member", new { id = user.UserId, pseudoName = user.PseudoName });
当重定向发生时,Url 最终看起来像http://site.com/Member/1?pseudoName=CEO
我究竟做错了什么?