简体   繁体   中英

Route to user's page under the dynamic username path in MVC4

I have some dynamic user route like

   routes.MapRoute(
            "UserNames", // Route name
            "{username}", // URL with parameters
            new { controller = "Home", action = "UserName" });

and under the HomeController.cs

public ActionResult UserName(string username)
        {
            ViewBag.Message = username;

           return RedirectToAction("Register","Account"); // Test...
        }

It is working fine.

But what I need is to get working the URL like

http:\\mywebsite.com\UserNameBob\MyGallery\1
http:\\mywebsite.com\UserNameBob\Profile
http:\\mywebsite.com\UserNameBob\MyFriends

How do I can archive it?

Any clu?

Thank you!!!

Do you mean something like this:

routes.MapRoute(
    "UserNames", // Route name
    "{username}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "UserName", id = UrlParameter.Optional });

And then in HomeController you put actions like these:

public ActionResult MyGallery(string username, int id) {
    // code
}

public ActionResult Profile(string username) {
    // code
}

EDIT: Of course, if the gallery ID is not an int, just use string or whatever is appropriate.

在ASP.NET中查找URL重写以在路由时处理动态参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM