.NET MVC Routing Issue - asp.net-mvc

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.

Related

Why on compile MVC starts on the wrong controller?

I have a Controller named HomeController, a folder named Home, and a View called Index. I also have another Controller named TestEditController, a folder named TestEdit, and a View called Index. For some reason, when I compile it the URL: http://localhost:4097/ doesn't point to Home/Index but to TestEdit/Index. I went to the Properties > Start Action > Specific Page ... and left the textbox blank. Note: putting a / doesn't work. I've cleaned, build, rebuild the project/solution. But still getting the same issue. Here's my Global.asax files:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"SectionsData", // Route name
"{controller}/{action}/{id}/{prodno}/{instid}/{section}", // URL with parameters
new { controller = "TestEdit", action = "Sections", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Sections", // Route name
"{controller}/{action}/{id}/{prodno}/{instid}", // URL with parameters
new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"MainProducts", // Route name
"{controller}/{action}/{id}/{prodno}", // URL with parameters
new { controller = "Home", action = "Main", id = UrlParameter.Optional, prodno = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Catalogs", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Products", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Your matching is too generic. try this instead:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"SectionsData", // Route name
"TestEdit/Sections/{id}/{prodno}/{instid}/{section}", // URL with parameters
new { controller = "TestEdit", action = "Sections", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Sections", // Route name
"TestEdit/Index/{id}/{prodno}/{instid}", // URL with parameters
new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"MainProducts", // Route name
"Home/Main/{id}/{prodno}", // URL with parameters
new { controller = "Home", action = "Main", id = UrlParameter.Optional, prodno = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Catalogs", // Route name
"Home/Products/{id}", // URL with parameters
new { controller = "Home", action = "Products", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
...but really, you don't need a lot of these routes.
Because both SectionsData and Sections Route has all other parameters optional, they will match before the default route.

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?

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.

How could this Routes Table be improved?

I have this RegisterRoutes method in Global.asax, can these be abbreviated with giving the same reslut? and what about their order?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Villages", // Route name
"villages", // URL with parameters
new { controller = "Villages", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"CreateVillage", // Route name
"villages/create", // URL with parameters
new { controller = "Villages", action = "Create", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Village", // Route name
"villages/{name}", // URL with parameters
new { controller = "Villages", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"", // Route name
"", // URL with parameters
new { controller = "Villages", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);
}
You can combine first with fourth route and second with third. The defualt route might also be combined with second and third. Note that you'll probably need to use some route constraints in order to make this table work properly. You can experiment with route debugger, but I would suggest you write unit tests for your routes.
Edit:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Villages", action = "List", name = UrlParameter.Optional }, // Parameter defaults
new { action = "create|list"}
);
routes.MapRoute(
"Village", // Route name
"villages/{name}", // URL with parameters
new { controller = "Villages", action = "Index", name = UrlParameter.Optional } // Parameter defaults
);

Simple ASP.NET MVC Routing question

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
);

Resources