User routing in ASP.NET MVC for urls like www.website.com/users/jeffAtwood

送分小仙女□ 提交于 2020-01-03 02:49:08

问题


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 }
        );
    }

回答1:


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}
);


来源:https://stackoverflow.com/questions/13207043/user-routing-in-asp-net-mvc-for-urls-like-www-website-com-users-jeffatwood

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!