0

I want to show my link in url like that:

http://localhost:60000/Admin/myControlerName/myActionName/3/7/2

This is my routConfig:

enter image description here

This is my contoller:

enter image description here

This is my action link:

enter image description here

Mylink shows in url like that:

http://localhost:60000/Admin/myControlerName/myActionName/?id1=3&id2=7&id3=2

But i want to show like that:

http://localhost:60000/Admin/myControlerName/myActionName/3/7/2

Where is my wrong? :(

1 Answer 1

2

You have 2 different issues here. First of all, it won't work right to make 3 optional parameters on a route. Only the last (right most) parameter can be optional. So, you need two routes to enable all of the combinations of 0, 1, 2, 3, 4, or 5 segments in the URL.

// This will match URLs 4 or 5 segments in length such as:
//
//   /Home/Index/2/3
//   /Home/Index/2/3/4
//
routes.MapRoute(
    name: "4-5Segments",
    url: "{controller}/{action}/{id}/{id2}/{id3}",
    defaults: new { id3 = UrlParameter.Optional }
);

// This will match URLs 0, 1, 2, or 3 segments in length such as:
//
//   /
//   /Home
//   /Home/Index/
//   /Home/Index/2
//
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Secondly, your ActionLink is not matching because you have specified the route value as id1, but in your route the value is id. So, you need to change ActionLink as follows.

@Html.ActionLink("my text", "myActionName", "myControllerName", new { id = 3, id2 = 7, id3 = 2}, null)

This assumes you have correctly set up your controller:

public class myControllerNameController : Controller
{
    //
    // GET: /myActionName/

    public ActionResult myActionName(int id = 0, int id2 = 0, int id3 = 0)
    {
        return View();
    }

}

See the working demo here.

For future reference, please provide the code as text. It is really hard to copy and edit an image, so you are much less likely to get an answer.

Sign up to request clarification or add additional context in comments.

9 Comments

For it to work, your controller name must be exactly myControllerNameController. I set it up in a test project, and it is working perfectly the way I specified it here.
Already my controller name as you said. Name must be like that ? name: "4-5Segments"
The name only applies if you are trying to match the route by name (for example, via RouteLink instead of ActionLink. Set up a new project with the default template and copy the controller, routes and ActionLink from my answer. Ensure that it works, then try to reconcile the differences between your current project and the new project until you find the issue. Most likely, you have another route in your solution that is matching 5 segments which is overriding this route. The first match registered always wins.
may u send your test project please :( [email protected] Thanks
I noticed my wrong new. I have area which called name 'admin'. This url success now but when i clicked actionLink, have requestQueryString as default. I mean that I have to edit url for this :(
|

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.