开发者

Is it possible to pass route value to show the name of the user without the controller name in MVC

I want to get the url for my site as "www.ABCD.com/username" without the controller name in MV开发者_如何学运维C architecture, such that when i click the name of the particular user i want to show details of the user with just the name of the user showing in url.


This should get you going in the right direction:

routes.MapRoute(
    "ViewProfile",                                              
    "{username}",                           
    new { controller = "User", action = "ViewProfile" },
    // new { username = "\w+" } // consider using a username regex here
);

Note that you will need to update the controller and action values to match your application. The ViewProfile action should look something like this:

public ActionResult ViewProfile(string username) { }

Keep in mind that the above route is very greedy and you will have to make sure any other actions which need to be accessed within your application are placed higher up in the route definitions list. Also keep in mind that if a user should pick a username that conflicts with a route you've defined above the ViewProfile route, the user's profile will never be accessible via the rooted path because the other route will override it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜