13

I've been working on a large MVC application over the past month or so, but this is the first time I've ever needed to define a custom route handler, and I'm running into some problems. Basically I have two parameters to pass. The first one is required and the second one is optional.

I'm following this answer here.

Here is my custom route:

routes.MapRoute(
    "MyRoute",
    "{controller}/{action}/{param1}/{param2}",
    new { 
        controller = "MyController", 
        action = "MyAction", 
        param1 = "", 
        param2 = "" // I have also tried "UrlParameter.Optional" here.
    }
);

And my action method signature:

public ActionResult MyAction(string param1, string param2)

If I try the URL http://[myserver]/MyController/MyAction/Test1/Test2 then it works like I expect it to, with param1 = "Test1" and param2 = "Test2"

If I try the URL http://[myserver]/MyController/MyAction/Test1 then both parameters are null.

Hopefully somebody can tell me what I'm doing wrong here, because I'm lost.

1
  • Hi from the future - try attribute routing now - WAY WAY easier and less error prone. Commented Nov 28, 2017 at 13:01

3 Answers 3

13

I assume that you created new route and left the default one that is very similar to yours. You should be aware that collection of routes is traversed to find first matching route. So if you have left the default one:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

above your route then it will match request to http://[myserver]/My/MyAction/Test1 and call MyController.MyAction and set "Text1" to parameter named id. Which will fail because this action is not declaring one named id.

What you need to do is to move your route as first in routes list and make it more specific then it is now:

routes.MapRoute(
            "Route",
            "My/{action}/{param1}/{param2}",
            new
            {
                controller = "My",
                action = "MyAction",
                param1 = "",
                param2 = ""
            });

This will force all traffic routed trough My to match this route.

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

1 Comment

No matter what I do I can't get multiple parameters to work in the path like this. If I try to create 2 parameters and request the page with a URL like My/MyAction/param1/param2 I get a 404 error. What am I doing wrong?
1

hi you create your rout like this i think this will hep you

routes.MapRoute(
                "Regis", // Route nameRegister
                "Artical/{id}", // URL with parameters
                new { controller = "Artical", action = "Show", id = UrlParameter.Optional } // Parameter defaults
            );

Comments

1

Try this

routes.MapRoute("MyRoute",
                 "myRoute/{param1 }/{param2 }",
                 new { controller = "MyController", action = "MyAction", param2 = UrlParameter.Optional },
                 new { param2 = @"\w+" });

you can specify one parameter as optional by using "UrlParameter.Optional" and specified second one with DataType means if you pass integer value then DataType (@"\d+") and for string i have mention above.

NOTE: Sequence of parameter is very important Optional parameter must pass at last and register your new route Before Default Route In Gloab.asax.

then you action link like

<a href="@Url.RouteUrl("MyRoute", new { param2 = "Test1",param1 = "Test2"})">Test</a>

OR with one parameter

  <a href="@Url.RouteUrl("MyRoute", new { param2 = "Test1"})">Test</a>

In you Controller

 public ActionResult MyAction(string param2,string param1)
 {
   return View()
 }

Comments

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.