0

I defined the below routes to have friendly urls in my RouteConfig.cs:

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

routes.MapRoute(
    name: "CreateChildRoute",
    url: "Admin/MaterialPaymentRequestSbuItem/CreateChild/{parentId}",
    defaults: new { controller = "MaterialPaymentRequestSbuItem", action = "CreateChild", parentId = UrlParameter.Optional }
); 
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new[] {"ProjectManager.Controllers"}

);

However when I call: http://localhost:46813/Admin/MaterialPaymentRequestSbuItem/CreateChild/23 A null value is passed to controller:

public ActionResult CreateChild(int? parentId){
  .....
} 

But calling http://localhost:46813/Admin/MaterialPaymentRequestSbuItem/CreateChild?parentId=32 Works with no problem and passes the parameter as it supposed to be.

P.S. Admin is a defined area in my application. Here is output of Route Debugger for http://localhost:46813/Admin/MaterialPaymentRequestSbuItem/CreateChild?parentId=2: enter image description here

7
  • The code you have shown works fine. How are you generating the urls? Commented May 31, 2016 at 0:55
  • @StephenMuecke I generated above URLs by hand to test application, however are there any other methods for tests? I tried RedirectToAction and stuff like this, which will create URLs in this pattern (.../CreateChild?parentId=32), as an example: @Html.ActionLink("Create New Item", "CreateChild", new { parentId = @ViewBag.ParentID }, null) Commented May 31, 2016 at 12:43
  • It would need to be @Html.ActionLink("Create New Item", "CreateChild", "MaterialPaymentRequestSbuItem", new { parentId = @ViewBag.ParentID }, null) which will work fine and generate ..../Admin/MaterialPaymentRequestSbuItem/CreateChild/23. If its not, then there is something else you have not shown us causing the problem. Commented May 31, 2016 at 12:50
  • @StephenMuecke this was brought from Index View of same controller so Adding controller name was not necessary however I tried to explicitly define Controller however it did not made any changes. Commented May 31, 2016 at 12:57
  • And your claiming its generating ...?parentId=32 instead of .../32? (I cant reproduce that) Commented May 31, 2016 at 13:01

1 Answer 1

4

From what I can see your route table looks to be correct, there's a typo in there but it seems to be pretty consistent throughout so I'm guessing that's not a problem.

Contrary to what teo van kot says you always want to start with the most specific routes first and have the more general ones come last as only the first route to match will be used.

A useful tool I've used to debug my routes is the Route Debugger from Phil Haak. It lets you see what route was chosen and, more usefully which parameters were mapped, and can be installed via NuGet

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

4 Comments

I put output of route debug, does it help you?
You've got another route in there that matches before the one your interested in. It's the default route for your Admin area, /Admin/{controller/{action/{id}. If you are going to use areas I suggest that you move your route from RouteConfig.cs into the config file for your area. It'll be in the RegisterArea method in the Areas\Admin\AdminAreaRegistration.cs file that you want all your admin routes to go in. And again it will need to come before the default route.
yeah, that is right, I had a default route for area in AdminAreaRegistration.cs :)
Awesome, just move your route into there and it should work as you want! If that's now working I'd appreciate you marking this as the answer :)

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.