我的目标是将URL路由如下:
http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page
解决方法
与KingNestor的实施类似,您也可以执行以下操作来简化您的工作:
1)写你的模型
public class MyUser{public String UserName{get; set;}}
2)添加到全局asax的路由
routes.MapRoute( "NameRouting","{name}",new { controller = "PersonalPage",action = "Index",username="name" });
3)滚动自己的自定义模型绑定器派生自IModelBinder
public class CustomBinder : IModelBinder { public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { var request = controllerContext.HttpContext.Request; var username = getUserNameFromDashedString(request["username"]); MyUser user = new MyUser(username); return user; } }
4)在你的行动中:
public ActionResult Index([ModelBinder(typeof(CustomBinder))] MyUser usr) { ViewData["Welcome"] = "Viewing " + usr.Username; return View(); }