I am using RegisterRoutes method in Global file to route user url to actual url.
routes.MapRoute("Blog",
"blog/dp/{id}",
new { controller ="Blog", action = "Details" });
As you might have guessed, Blog is the controller and Details is its action.
So the problem is that the following code does not generate my desire URL which has dp word in its url.
By the way, I don't want to change my Action Name.
#Html.ActionLink( "headline", "Details", "Blog", new { id="1200" }, null )
Thanks in advance ;)
You could use a RouteLink instead of using an ActionLink. In a RouteLink you can explicitly pass your route name:
#Html.RouteLink("headline", "Blog", new { controller = "Blog", action = "Details" })
More about RouteLink method can be found in the MSDN. Reordering routes can do the job but that's a rather fragile mechanism.
The ActionLink may be using a different route from what you are providing in the question. Do you have any routes declared before this one?
The routes are matched in the order that you provide them. So, for instance, if you had something like:
routes.MapRoute("Blog",
"blog/dp", // {id} is not provided in this one
new { controller ="Blog", action = "Details" });
declared before this route:
routes.MapRoute("Blog",
"blog/dp/{id}",
new { controller ="Blog", action = "Details" });
The first route would be matched first and you would see /blog/dp/?id=1200 instead of /blog/dp/1200.
Related
I have Created a new Controller called Consultants. Then I create action method Index()..
I gave route like the following,
routes.MapRouteLowercase(
"consultants",
"consultants/index",
new { controller = "Consultants", action = "Index" }
);
In view, ActionLink method is,
<%: Html.ActionLink("Consultant Home", "Index", "Consultants", null, new { title = "Back home" })%>
But it is not routing. It is showing Resource cannot be find
Please correct my issues...
It looks like you've created a custom RouteCollectionExtensions called MapRouteLowercase (or at least I'm not familiar with it). I'd test that to make sure it's working as you expect by changing your route to this:
routes.MapRoute(
"consultants",
"consultants/index",
new { controller = "Consultants", action = "Index" }
);
Otherwise, you may have another route map causing issues, so make sure that route config is at the very top of your routing. Order plays an important role in how the route engine determines the correct url when searching for patterns. So order from specific to general.
For example, if you did something like this, it would cause issues with your current route:
routes.MapRoute(
"dateRoute",
"consultants/{date}",
new { controller = "Consultants", action = "Dates", date = UrlParameter.Optional }
);
I have a controller called TaskListsController with an action called Edit.
The Edit action accepts a nullable int parameter. If it receives null, it adds a TaskList. If it receives an int, it edits the TaskList with the same ID. The logic is almost identical in each case.
I would like to configure my routing in such a way that the URL 'TaskLists/Add' maps to the Edit action with null as the parameter, and the URL 'TaskLists/Edit/{id}' maps to Edit and passes it the ID in the URL.
I can get this to work from the point of view of entering URLs in the browser and having them routed to the action correctly, but where it's falling down is where the system generates URLs from Html.ActionLink.
I am generating 'Add' links using:
Html.ActionLink("Add task list", "Edit", "TaskLists")
And I'm generating 'Edit' links using:
Html.ActionLink(taskList.Name, "Edit", new { Id = taskList.Id })
..where taskList is an instance of the TaskList class.
If I use this routing:
routes.MapRoute(
"TaskLists/Add", // Route name
"TaskLists/Add", // URL with parameters
new { controller = "TaskLists", action = "Edit" });
routes.MapRoute(
"TaskLists/Edit/{id}", // Route name
"TaskLists/Edit/{id}", // URL with parameters
new { controller = "TaskLists", action = "Edit", id = UrlParameter.Optional });
...the 'Add' link is generated correctly ('TaskLists/Add') but the 'Edit' link comes out 'TaskLists/Add?Id=1'.
If I put the routing commands the other way around:
routes.MapRoute(
"TaskLists/Edit/{id}", // Route name
"TaskLists/Edit/{id}", // URL with parameters
new { controller = "TaskLists", action = "Edit", id = UrlParameter.Optional });
routes.MapRoute(
"TaskLists/Add", // Route name
"TaskLists/Add", // URL with parameters
new { controller = "TaskLists", action = "Edit" });
...then the 'Edit' links are generated correctly ('TaskLists/Edit/x'), but the 'Add' links comes out 'TaskLists/Edit'.
Is there a way I can have my cake and eat it?
Using named routes (Html.RouteLink("linkText", "routeName")) could be a cleaner way of defining this, as then you're dealing with a clean route in the view also. This would also mean that you will hit the correct route every time without worry.
Update the Name parameter on your routes accordingly, then add the following to your view:
Html.RouteLink("Add task list", "NewTaskList")
and
Html.RouteLink(taskList.Name, "EditTaskList", new { Id = taskList.Id })
I'm having problems when using this route:
routes.MapRoute("ProductIndex", "pr-{key}", new { controller = "Home", action = "Product" });
When the key contains 'pr-', the route doesn't work.
Example: http://.../pr-my-product-key-with-pr-key
Routes don't work the way you want them to work, but it's easily converted
change your route to:
routes.MapRoute(
"ProductIndex",
"pr/{key}",
new { controller = "Home", action = "Product" });
and use:
http://.../pr/my-product-key-with-pr-key
or, if you really want to use that "way" you need to override the Initialization method of your main controller are check the link with StartWith() and redirect to the proper Controller.
I have these two routes:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default2", // Route name
"{controller}/{action}/{OrderId}/{CustomerID}", // URL with parameters
new { controller = "NorthwindOrders", action = "Index", OrderId = UrlParameter.Optional, CustomerID = UrlParameter.Optional } // Parameter defaults
);
and want to create link that uses the second route.
How can I do this?
If you want to specifically use a route, you can use the Html helper Html.RouteLink :
<%= Html.RouteLink("my link", "Default2", new {OrderId=1, CustomerId=2}) %>
Also, you can put the second route first : the most generic route should be at the end, in order to be used only when no specific route was found.
Order is very important w/ MapRoutes. Try reversing the order of those two statements.
As well you should use definition of routes in different order.
the order should be more specific first to less
also in your action links i would add routing parameters
Actually not 100% what the OP asked for, but what I needed was an ActionResult that forcefully uses a specific route. RedirectToAction always used the wrong route. I found it in RedirectToRoute("RouteName", new { action="Index" }).
I've been using this some while now, but I can't seem to figure out, where could be mistake in this simple code:
<a href="<%= Url.Action("Page", new { page=(Model.PageIndex + 1) }) %>" >a</a>
With this routing table:
routes.MapRoute(
"Paging",
"Home/Page/{page}",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
And of course this method
public ActionResult Index(int? page)
I am getting instead of expected address http://localhost:58296/Home/Page/1 the one http://localhost:58296/Home/Page?page=1
When using
<%= Html.RouteLink("a", "Paging", new { page=(Model.PageIndex+1) }) %>
it works.. Please, where is my mistake? I want image link, so if there is a way to insert it into Html.RouteLink, I would appriciate that info too.
Thanks in advance.
There's a bunch of items to cover here for you to fully understand what's happening. Sorry, this will be a bit long.
routes.MapRoute(
"Paging",
"Home/Page/{page}",
new { controller = "Home", action = "Index" }
);
First, this is the route you want to hit. You do not include the action route parameter, '{action}', in the route path. The only action this route can possibly take is the value you have specified as the default action value "Index".
<a href="<%= Url.Action("Page", new { page=(Model.PageIndex + 1) }) %>" >a</a>
Second, in your link you are setting an action of Page. The route you are expecting does not accept action as a parameter and the only action it is aware of is Index. When Url.Action looks for possible routes in your route table, it will skip the route you want because that route does not accept an action of Page. The default route is valid though because you are implicitly supplying a controller, Home, explicitly supplying an action, Page, allowing the framework to supply a default for id, string.Empty, and any other parameters are tacked on as query parameters, page.
When you changed the Url.Action to "Index", the Url.Action method inspected the routes table and found the route with the action of Index specified for the Home controller with a parameter of page and everything is happier.
Hope that helps and isn't too confusing.
I don't know why, but
<%= Url.Action("Index", new { page=(Model.PageIndex + 1) }) %>
works and it displays and directs to /Home/Page/1 . If someone could explain it to me, I would be gratful.