ASP.net MVC RenderAction and Route?

The problem is that you can't have non optional parameters after an optional parameter.

Up vote 1 down vote favorite share g+ share fb share tw.

I have a web project using ASP.net MVC3. There's an child action in my project. I use to call a shared action.

But I find that if my current url is "localhost/application", it throws an exception "No route in the route table matches the supplied values". But when current url is "localhost/application/index", it works fine. Index is a default action in my route config, which is shown below: public static void RegisterRoutesTo(RouteCollection routes) { routes.

IgnoreRoute("{resource}. Axd/{*pathInfo}"); routes. IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.

Ico(/. *)? " }); //routes.

IgnoreRoute("{*chartName}", new { chartName = @"(.*)?Chart. Aspx" }); //ignore request for charting request routes. Ignore("{*pathInfo}", new { pathInfo = @"^.

*(ChartImg. Axd)$" }); routes. MapRoute( "Default", // Route name "{controller}/{id}/{action}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.

Optional }, // Parameter defaults new { httpMethod = new HttpMethodConstraint("GET", "POST") } // Allowed http methods ); } Note that I switch default id and action position. I see mvc can remove the default controller and action name in url when using "Html. ActionLink(...)".

And I don't like to use explicit url string in my views. How can make it work? My Action code is simple: ChildActionOnly public ActionResult Navigator() { return PartialView(appFacility.GetAll()); } Thanks alot.

Asp. Net-mvc-3 link|improve this question asked Apr 23 '11 at 5:34Chris Lee224110 45% accept rate.

Wrong route URL definition and defaults combination The problem is that you can't have non optional parameters after an optional parameter. Why does localhost/application/index work? This are route values: controller = "application" (supplied from URL) id = "index" (supplied from URL) action = "Index" (supplied as route default) Basically these values equal to localhost/application/index/index request URL.

If you'd like your RenderAction to work, you'd have to call it this way: which would equal to localhost/application/0/navigator request URL. But you'll soon find out that your route doesn't work and you'll have to change it (because I suppose you don't like having that additional 0 in your URL). If you provide information how you'd like your route work (or why you've decided to switch action and id) we can provide an answer that will help you meet your requirements.

Thanks Robert, I think it is the optional parameter problem. About the url, it is the perference of our team. We more prefer /book/{id}/view than /book/view/{id}.

ASFAIK, Stackoverflow is built up with asp.net mvc, and the url is like "question/ask" and "question/12345/edit". I think its more "RESTful". I said that because there're a lot of restful actions result in my project for rich UI.

To solve this, I add another route map: "{controller}/{action}/{id}", make id optional, and make the id in "{controller}/{id}/{action}" required. I don't know if it's right but it works ok for now. – Chris Lee Apr 25 '11 at 6:18.

Optinal parameters work correctly only on the end of route. Try something like this: routes. MapRoute("DefaultWithID", "{controller}/{id}/{action}", new { action = "Index" }, new { id = "^0-9+$" } ); routes.

MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index" } ); edit: hopefully fixed :) this version counts on fact that ID will be numeric - without constraint we can't tell whether it would mean action or id, so there couldn't be default action on routes when ID is specified.

I don't think this will work, because the first route will match request of the second route as well. It will just route to Index action with some id along with it. Basically second route will never get hit.

– Robert Koritnik Apr 23 '11 at 8:37 You're right, gimme me a minute :) – Lukáš Novotný Apr 23 '11 at 8:38 Allright, I edited my answer. The only way to make optional parameter on the begining or in the middle of route is to use 2 routes and constraint on one of them – Lukáš Novotný Apr 23 '11 at 8:50 Thanks Lukas, I did much like your way. Add a default map route for non-id request – Chris Lee Apr 23 '11 at 8:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions