Why on compile MVC starts on the wrong controller? - asp.net-mvc

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.

Related

Routing issue in Global file

Routes Information
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
routes.MapRoute(
"Default1", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Url Information
http://localhost:24060/home/22323 //Failed
http://localhost:24060/home/index/22323 //Passed\
Query, How can i pass both url ?
You have to map the default route last. Also you should create a constraint in the other route to not to block the default route.
routes.MapRoute(
"Default1",
"{controller}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { id = #"\d+" });
//second segment has to be an integer, otherwise skip this route.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });

How do I access the id parameter from the ActionLink?

The issue is that id = 5 is already in the URL as http://localhost:4032/Category/5. But to pass that value to the ActionLink it seems I have to do this:
<td> #Html.ActionLink(p.ProdNo, "Main", "Home", new { id = 5, prodno = p.ProdNo }, null) </td>
Which results in the correct URL: http://localhost:4032/Main/5/1097
But having to do that doesn't seem very right. I know there must be some clever way to handle this. Unfortunately, it's late in the day and I'm all out of clever.
I tried this:
<td> #Html.ActionLink(p.ProdNo, "Main", "Home", new {prodno = p.ProdNo }, null) </td>
But ended up with http://localhost:4032/Main?prodno=1097.
I tried adding the parameters to the corresponding method in the codebehind but that didn't seem to work either.
So in short, when using an ActionLink how do I get the routevalue already in the URL AND pass in another/new routevalue?
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Sections", // Route name
"{controller}/{action}/{id}/{prodno}/{instid}/{section}", // URL with parameters
new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, prodno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Pumps", // Route name
"{controller}/{action}/{id}/{prodno}", // URL with parameters
new { controller = "Home", action = "Main", id = UrlParameter.Optional, prodno = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Jobs", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Jobs", 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
);
}
I think you need to set up the route for your prodno-parameter. Looks like this thread could contain a solution:
Routing with Multiple Parameters using ASP.NET MVC
You could fetch it from the RouteData:
#Html.ActionLink(
p.ProdNo,
"Main",
"Home",
new { id = ViewContext.RouteData["id"], prodno = p.ProdNo },
null
)

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

Resources