How to handle routing with two actions with the same number of paramaters in MVC3?

Possible Duplicate:
ActionLink to show parameters in URL instead of querystring?

I have the following routes:

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

I am trying for:

/home/list/1/a
/home/details/2/b

The above results in home/details/2?name=b

-------------Problems Reply------------

Assuming you haven't gotten your code example wrong, You can't.

The route handler will pick the 1st route that matches.

However, from what it looks like what you actually want is this:

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

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

In fact, those two are similar enough that it can be distilled into 1 route

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

Avoid creating a route with two UrlParameter.Optional declarations.

You can achieve your routing by adding one route above the default route, like so:

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

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

The first route will create the URL you want for when both variables are declared. The second route will work for either one variable or no variables.

Category:asp.net mvc Views:0 Time:2011-09-21

Related post

  • ASP.MVC routes without Details action 2010-03-09

    I'd like to have URLs that are even shorter than /{Controller}/{Action}/{Id}. For example, I'd like {Controller}/{Id}, where {Id} is a string. This would allow for simple paths, e.g. Users/Username, Pages/Pagename, News/Newsname. I like this better t

  • Routing to the actions with same names but different parameters 2010-04-12

    I have this set of routes: routes.MapRoute( "IssueType", "issue/{type}", new { controller = "Issue", action = "Index" } ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", actio

  • How to route to different actions depending on the request method in Rails? 2011-01-07

    As we all know, a simple resources :meetings will generate 7 actions for me. Two of these are index and create. A really cool thing about these two!: The URL for both is /meetings, but when I GET /meetings I am routed to the def index action and when

  • MVC route with default action and parameter, several actions by controller 2012-02-29

    I m trying to achive route like that: http://mysite.com/portfolio/landscape http://mysite.com/portfolio/friends etc... so I wrote that: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); rout

  • ASP.Net MVC - how to handle exception in JSON action (return JSON error info), but also publish the exception for filters? 2009-04-10

    I'm using a filter to log exceptions thrown by actions which looks like this: public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Exception != null) { //logger.Error(xxx); } base.OnActionExecuted(filterConte

  • Rails, routing many named routes to one action 2010-02-17

    Is there a simpler way of writing this: map.old_site_cusom_packages '/customs_packages_options.html', :controller => :public, :action => :redirect_to_home map.old_corporate '/corporate.html', :controller => :public, :action => :redirect_t

  • Routing and Controller Actions - Optional Data 2010-11-15

    I created a route in ASP.net MVC application that looks like this: routes.MapRoute( "ArticleRoute", "{year}/{month}/{day}/{articleId}/{displayName}", new { controller = "Article", action = "Details" } ); I want a route that will be like a blog post a

  • ASP.Net MVC Routing with HTTP Action Verbs 2011-05-17

    I am writing a implementing a ASP.NET web app, and when I call against a particular url, I want to call a different action method depending on whether the request is a GET or a POST. I've tried two different approaches - first of all I start with a c

  • How to register minimum routes for similar actions? 2011-07-02

    I have some actions: public partial class MyController : Controller { public ActionResult Action1() { } public ActionResult Action2(int id) { } public ActionResult Action3(string id) { } public ActionResult Action4(string name) { } } Do I need regist

  • ZF routing based on action name when controller name is not provided? 2012-01-17

    I want to route non-existent controllers to (when applicable) actions of the index controller. Is this possible? Example: IndexController fooAction When I acces http://host/foo/, I want to be directed to http://host/index/foo Is this possible? ------

  • How do you conditionally route to an action if the request originates from an AJAX call? 2012-02-20

    Using APS.NET routing, how do you conditionally route to separate actions based on whether or not the request originated as an AJAX call? For instance on a controller I may have two actions: public ActionResult List() { return View(); } and public Ac

  • Create a Route Constraint that only applies a route when the action has a particular action filter 2009-05-14

    I have a list of actions on various controllers that are 'Admin' functions (create, update, delete) but other actions on those same controllers that aren't admin actions. What I want to do is create a route that will prefix /Admin/ before all urls th

  • AngularJS - handle routes before app startup 2014-09-26

    Is it ok to use a Service within $stateProvider, for this purpose? I have been through a lot of posts on stack regarding routing but most are confusing for beginners as everyone uses a different approach and treat it like rocket science. I would like

  • ASP.NET MVC retrieving route to controller, action and parameters 2009-04-10

    In ASP.NET MVC how can I generate a URL to a specific controller and action along with other parameters. The RedirectToAction allows me to redirect, but I would like to retrieve this URL instead from within my controller. I tried using: Url.Action("P

  • Route Links - Url.Action 2010-03-15

    I'm trying to return my links so they display as /Area_1419.aspx/2/1. I've managed to get that result in example 2 but I don't understand why it works, as I would exspect example 1 below to work. I don't see how Example 2 knows to go to the Area_1419

  • Ruby-on-rails: routing problem: controller action looks for show when it should look for finalize 2010-03-27

    background: trying to use the twitter gem for ruby-on-rails. in routes: map.resources :twitter_sessions map.finalize_twitter_sessions 'twitter_sessions/finalize', :controller => 'twitter_sessions', :action => 'finalize' (twitter_sessions is the

  • problem with routing/T4MVC Url.Action() 2010-04-15

    I have these 2 routes : routes.MapRoute("Agenda", ConfigurationManager.AppSettings["eventsUrl"] + "/{year}/{month}", MVC.Events.Index(), new { year = DateTime.Now.Year, month = DateTime.Now.Month }); routes.MapRoute("AgendaDetail", ConfigurationManag

  • Rails routes direct index action to show action 2010-04-20

    So I created some rspec_scaffold for an Exercise model and added "map.resource :exercises" to my routes file and I was surprised when the "/exercises" url rendered the show action. What the heck? Why doesn't that render the index action? rake routes

  • Rails: Problem with routes and special Action 2010-06-07

    Sorry for this question but I can't find my error! In my Project I have my model called "team". A User can create a "team" or a "contest". The difference between this both is, that contest requires more data than a normal team. So I created the colum

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.093 (s). 11 q(s)