用户路由在ASP.NET MVC像www.website.com/users/jeffAtwood网址 [英] User routing in ASP.NET MVC for urls like www.website.com/users/jeffAtwood
问题描述
我想显示在以下网址用户的详细信息:
www.website.com/users/yasser
在这里的最后一个条目亚西尔·
是用户名我已经尝试了几个途径,但它只是NT工作。
我的用户控制器,如下图所示。
公共类UserController的:控制器
{
公众的ActionResult指数(用户名字符串)
{
VAR模型= _service.GetUserDetails(用户名);
返回查看(模型);
}
}
我已经下文称<一个href=\"http://stackoverflow.com/questions/3000547/asp-net-mvc-user-routing-like-that-in-stackoverflow\">this和其他的一些环节,但我真的无法弄清楚它是如何工作。
有人能帮助我在这。谢谢
修改
我目前的航线配置低于
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
}
执行路线从顶部到底部:
routes.MapRoute(用户配置
用户/ {用户名},
新{控制器=用户,行动=指数,用户名=的String.Empty}
);routes.MapRoute(默认,
{控制器} / {行动} / {ID}
新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
I am trying to show user details at the following url :
www.website.com/users/yasser
where the last entry yasser
is the username I have tried a couple of routes but it just does nt work.
My User controller is as shown below.
public class UserController : Controller
{
public ActionResult Index(string username)
{
var model = _service.GetUserDetails(username);
return View(model);
}
}
I have reffered this and couple of other links, but I really could not figure out how it worked.
Can some one help me out on this. Thanks
Edit :
My current route config is below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Routes executes from top to the bottom:
routes.MapRoute("UserProfile",
"Users/{username}",
new { controller = "User", action = "Index", username = string.Empty }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
这篇关于用户路由在ASP.NET MVC像www.website.com/users/jeffAtwood网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!