简体   繁体   中英

I want Create Url like in www.mysite.com/home/details/1/lesson1 in mvc with Url.action

I want create url in mvc with Url.Action

my code is

Url.Action( "Details", "Home",  new   { id= item.Id  ,title=item.Title},  "http" )

but this code create link like in

http://localhost:45201/Admin/Home/Details/1?title=lesson1

I want like in

http://localhost:45201/Admin/Home/Details/1/lesson1

Depends on how you define your routes.

use this for convention-based routing

routes.MapRoute(
    name: "AdminHomeDetails",
    url: "Admin/Home/Details/{id}/{title}",
    defaults: new { controller = "Home", action = "Details" }
);

OR use this for attribute routing:

[RoutePrefix("Admin/Home")]
public class HomeController : Controller {
    //GET Admin/Home/Details/1/lesson1
    [Route("Details/{id:int}/{title}")]
    public ActionResult Details(int id, string title) { ... }
}

for attribute routing don't forget to map attribute routes before convention-based routes

public class RouteConfig {

    public static void RegisterRoutes(RouteCollection routes) {

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        //...and then convention-based routes.

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index" }
        );
    }
}

Check Attribute Routing in ASP.NET MVC 5 for more on attribute routing.

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