Simple ASP.NET MVC Routing question - asp.net-mvc

I have two pages in my simple MVC App with two defined routes:
routes.MapRoute(
"Results", // Route name
"Results/{id}", // URL with parameters
new { controller = "Results", action = "Index",
id = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Main", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
I needed to have the results page load with just a product ID such as this: [MyDomain....]/Results/12345. But also the main page does a POST (using JQuery) to the Results Controller for updates using this route: [MyDomain....]/Main/Update along with a data bag. This works fine when I only have the "Default" route. But when I added the other "Results" route, all the POST calls to update are failing. Any ideas what I'm doing wrong???
Thanks a lot.

I didn't try this out, but should accomplish what you need. Not sure if there may be a "better" way to accomplish it.
routes.MapRoute(
"Results", // Route name
"Results/{id}", // URL with parameters
new { controller = "Results", action = "Index", id = "" } // Parameter defaults
new { id = #"\d+" } // regex for id param - id must be a number
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Main", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Related

.NET MVC Routing Issue

For some reason, my route isn't being seen by .NET.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"RSSFeed", // Route name
"Blog/RSSFeed", // URL with parameters
new { controller = "Blog", action = "RSSFeed", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"About", // Route name
"About/", // URL with parameters
new { controller = "About", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Contact", // Route name
"Contact/", // URL with parameters
new { controller = "Contact", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Admin", // Route name
"admin/", // URL with parameters
new { controller = "Admin", action = "CreatePost", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Resume", // Route name
"resume/", // URL with parameters
new { controller = "Resume", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"ViewArticle", // Route name
"{controller}/{action}/{id}/{friendly}", // URL with parameters
new { friendly = "" } // Parameter defaults
);
routes.MapRoute(
"DownloadResume", // Route name
"DownloadResume/", // URL with parameters
new { controller = "Resume", action = "DownloadResume", id = UrlParameter.Optional } // Parameter defaults
);
//Any of these
routes.MapRoute(
"cpe", // Route name
"Play/CreatePlayEvent/{groupid}/{username}/{activeplay}/{game}", // URL with parameters
new { controller = "Play", action = "CreatePlayEvent", groupID = "0", username = "", activeplay = "", game = "" } // Parameter defaults
);
routes.MapRoute(
"CheckPlayEvent", // Route name
"{controller}/{action}/{groupID}", // URL with parameters
new { controller = "Play", action = "CheckPlayEvent", groupID = "0" } // Parameter defaults
);
routes.MapRoute(
"clpe", // Route name
"Play/ClearPlayEvent/{groupid}/{username}", // URL with parameters
new { controller = "Play", action = "ClearPlayEvent", groupID = "0", username = "" } // Parameter defaults
);
//End broken routes
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
myurl.com/Play/CheckPlayEvent/0
Group ID keeps being seen as NULL on the controller side. Anyone? Also, there doesn't seem to be a built-in debugger for routing, or at least I haven't seen one. What does everyone use to debug routes?
Is there a way to say that the Controller name, method name, and parameters make the URL?
I think the following route is getting hit, rather that your intended route:
routes.MapRoute(
"ViewArticle", // Route name
"{controller}/{action}/{id}/{friendly}", // URL with parameters
new { friendly = "" } // Parameter defaults
);
Friendly is optional, so {controller}/{action}/{id} style routes will also hit this - which looks really quite like the route you are having trouble with.
Move your play route above this one and try again.

MVC: Multiple Routes for 1 Controller

I want users to be able to access the "/Linecard" page of my ASP.Net MVC site using "/Linecard" or "/Manufacturers" as the URL... so same controller, 2 different possible URLs.
I tried adding the following:
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
Adding this after the "Default" route doesn't work at all and I get a 404 error when I go to "/Manufacturers". Putting it BEFORE "Default" works, but then only "/Manufacturers" shows up in the URL when I click menu links since it is the first match. I would like "/Linecard" to always show as the URL.
Any pointers? Is there a certain constraint I can use to accomplish this? Thanks!
I had the same problem when we moved to extension-less URLs. We needed to continue to support one route with extensions. I got around it by having my default route apply to everything except the old URL, then after that mapping one specifically for the exception
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
// if controller specified does not match 'manufacturers' (case insensitive)
new { controller = "^((?i)(?!manufacturers).)*$" },
new string[] { "Namespace.Of.Controllers" }
);
routes.MapRoute(
"Manufacturers", // Route name
"Manufacturers/{action}/{id}", // URL with parameters
new { controller = "Linecard", action = "Index", id = UrlParameter.Optional },
new string[] { "Namespace.Of.Controllers" }
);
You could also set an order when mapping your routes with the defaults at the end like so
routes.MapRoute(
name: "Manufacturers",
url: "Manufacturers/{action}/{id}",
defaults: new { controller = "Linecard", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

ASP.NET MVC3 not right index page because of routes

I have these routes:
routes.MapRoute(
"ActionOnly",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { action = "Klub|Historie" });
routes.MapRoute(
"Administrace", // Route name
"Administrace/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// Hrac/Jmeno_hrace
routes.MapRoute(
"Hrac",
"Hrac/{name}",
new { Controller = "Hrac", Action = "Name" }
);
// pro aktivaci uzivatele který se registroval, ale jeste nepotvrdil email
routes.MapRoute(
"Activate",
"Account/Activate/{username}/{key}",
new { controller = "Account", action = "Activate", username = UrlParameter.Optional, key = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Why #Html.ActionLink("Domů", "Index", "Home") is creating website.com/Administrace/Home and not website.com/Home/index and how can I fix it?
Your Administrace route is swallowing all controllers.
You should change it to hard-code the controller name:
routes.MapRoute(
"Administrace", // Route name
"Administrace/Administrace/{action}/{id}", // URL with parameters
new { controller = "Administrace", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
If you want that route to work for multiple controllers, you should replace it with an area (or just add a constraint).
Use #Html.RouteLink instead with the route name being default
Link to something that might help:
What's the difference between RouteLink and ActionLink in ASP.NET MVC?

Why doesn't my routing work?

Im trying to set up an alternative route on my application...
routes.MapRoute(
"x", // Route name
"{controller}/{action}/{datetime}", // URL with parameters
new { controller = "Home", action = "Index", datetime = UrlParameter.Optional } // Parameter defaults
);
I have the action...
public ActionResult GetBlogsByMonth(string datetime)
{
if (datetime!= null)
{
IList<BlogModel> blogs = (IList<BlogModel>)manager.GetBlogsInMonth(DateTime.Parse(datetime)).ToList();
return View(blogs);
}
else
{
return View();
}
}
But when I put the debugger on the action the datetime is always null... :-(
Probably, your request has been caught by another route. Make sure to put your route at the top when you are registering them.
For example, if you are using this route with the default one, the default one will catch the request, not your custom route if you reference them in the following order:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"x", // Route name
"{controller}/{action}/{datetime}", // URL with parameters
new { controller = "Home", action = "Index", datetime = UrlParameter.Optional } // Parameter defaults
);
As for the solution, as #Darin suggested, you need to define a constraint because if you put your custom one in front, this time the default one will never be hit.
routes.MapRoute(
"x", // Route name
"{controller}/{action}/{datetime}", // URL with parameters
new { controller = "Home", action = "Index", datetime = UrlParameter.Optional }, // Parameter defaults
new { datetime = #"^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The below URLs will be caught by your custom route:
/Poo/Bar/2011-11-31
/Poo/Bar/2011-01-04
/Poo/Bar/2011-01-04
You can change the RegEx for your needs.
When constructing a link to your action you can use a RouteLink instead of an ActionLink. With the RouteLink you can pass a named route name to force the right route is chosen to construct the link. For your example the link should look somehow like this:
#Html.RouteLink("Blog Posts...", "x", new { controller="Blog", action="GetBlogsByMonth" datetime = THEDATETIME })
Hint: You can use the DateTime type in your action as parameter instead of a String type so you can avoid the unnecessary call to DateTime.Parse

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

This question already has answers here:
Closed 11 years ago.
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
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.

Resources