ASP.NET MVC3 not right index page because of routes - asp.net-mvc

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?

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

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.

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
)

MVC 3 Route problem

ActionLink result "http://localhost:5089/Article/GetArticlesByCategory?category=ASP.NET&categoryId=2". i want to show that link type "http://localhost:5089/Blog/ASP.NET". what is wrong route named "Article".
Routes:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Article",
"Blog/{category}", //
new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional, categoryId = UrlParameter.Optional }
Link:
#Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name, categoryId = k.CategoryId }, null)
SOLVED
GetArticlesByCategory parameter int categoryId changed to >> string category and replaced action codes as to new parameter (string category)
Routes replaced with:
routes.MapRoute(
"Category",
"Blog/{category}",
new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index", id = UrlParameter.Optional } // Parameter defaults
);
ActionLink replaced with:
#Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name }, null)
There are a few issues. First, and most important, your routes are specified in the wrong order. The default route should be defined last. Second, never define a route with two optional parameters. It just causes too many problems.
Try the following for your routes:
routes.MapRoute(
"CategoryAndId",
"Blog/{category}/{categoryId}",
new { controller = "Article", action = "GetArticlesByCategory" }
);
routes.MapRoute(
"CategoryOnly",
"Blog/{category}",
new { controller = "Article", action = "GetArticlesByCategory",
category = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index",
id = UrlParameter.Optional } // Parameter defaults
);
You are not specifying the action in the route
routes.MapRoute(
"Article",
"Blog/{action}/{category}/{categoryId}", //
new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional, categoryId = UrlParameter.Optional }
I suggest you use Phil Haack's routes debug, http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx. A great way for debugging your MVC routes
If you want the link to show as http://localhost:5089/Blog/ASP.NET, you'll need to change the actionlink as such:
#Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name }, new { #title = "Kategorisindeki Makaleler", #class = "selected" })
Since you don't want the CategoryID in the link, there is no need to put it in. the route isn't being matched by the actionlink because it expects a CategoryID parameter as well
EDIT
If you want the CategoryID to be read from the route, it needs to be added to the route. otherwise it will just be appended as a parameter (like in your original example).
If you change your route to:
"Blog/{categoryId}/{category}"
or
"Blog/{category}/{categoryId}"
The link will now look like Blog/2/ASP.NET or Blog/ASP.NET/2 but if you want the categoryId to be read from the URL, then I don't think you have much choice

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