0

I have two routes, the default one

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

I added another route, sometimes the parameter will by a string

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

When I use the "ByName" route in a RouteLink, the URL is fine, but the parameter in my controller is empty

In the view:

@Html.RouteLink(application.Nom, "ByName", new {controller= "Packaging", action = "EditApplication", name = application.Nom})

The controller

public ActionResult EditApplication(string name)

URL result is fine: .../Packaging/EditApplication/VisualStudio, but the parameter value stays null. Why?

Thank you

1 Answer 1

1

You cannot have TWO Routes with same parameters and same definition, first one will take precedence. Instead, you need to have something like shown below with specific constraints in routes.

 routes.MapRoute(
        name: "ByName",
        url: "sample/{action}/{name}",
        defaults: new { controller = "Home", action = "Index", name = UrlParameter.Optional }
        );

 routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed this works, you would think that named routes would get over this, but it does not. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.