0

I have this route declaration:

routes.MapRoute(
                // Route name
                "WhiteLabelPartners",
                // URL with parameters
                "partners/{partnerName}/{controller}/{action}/{id}",
                // Parameter defaults
                new { partnerName = "", controller = "", action = "index", id = UrlParameter.Optional }
                );

When I try this URL:

/partners/a/savings/index/1

...it works fine. The index action of the Savings controller is hit.

But, when I try this URL:

/partners/a/savings/index

I get a "not found".

If I have a UrlParameter.Optional for the {id} parameter, why is it still being required?

Could anyone explain? How can I make the {id} parameter optional?

Thanks

8
  • If you request /partners/a/savings/index/ does it work? Commented May 17, 2011 at 19:13
  • No Tejs, it doesn't work, I get a "not found" too. Commented May 17, 2011 at 19:14
  • Specify a default controller in your route defaults. It should land you on the default controller if the route is matching. Commented May 17, 2011 at 19:15
  • can you post your action method? Commented May 17, 2011 at 19:16
  • I tried that but it didn't work. Very strange. Commented May 17, 2011 at 19:17

1 Answer 1

0

Make sure your Index Action does not expect a parameter.

If, on your controller, your Index action looks like this : public ActionResult Index(int id)

it will need a param to be passed in the ID field, as you are not providing a default value in the route. That's probably why you are getting the 'not found'error as it cannot find a matching action. It is expecting :

public ActionResult Index()

You could leave the Index() action without a parameter and from within the Index() action, retrieve the value of the passed in 'id' parameter, if any via :

RouteData.Values["id"]

to use it. Let us know if that works for you.

(note: i wanted to post a comment like tejs(but dont see a link to add comment?!), to ask you to show your Index method signature on the controller, so please do include that in the question.)

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

3 Comments

you can use public ActionResult Index(int? id) - this make id optional
Thanks. I think my problem is something with the {partnerName} parameter. When I attach the debugger and try the URL /partners/a/savings/index, I get an exception, Not found. When inspecting the requestContext.RouteData.Values, I can see keys "controller", "action" and "id". Then "a", "savings" and "index" as values. It's not recognizing the "partnerName" parameter. I changed my action method to: public virtual ActionResult Index(string partnerName, string id) {} but still no luck.
Found the problem: there was an Area registered with the same name of "partners" and it was being chosen, for the route, instead of my declared route.

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.