'url.Action(controller, action) not routing as expected
In my ASP.NET MVC 3, I am using this code
<a [email protected]("myController", "myaction")>
But when I click on it, it does't go to my action. Instead, in the URL I see this
http://localhost:1402/?Length=2
Am I missing something?
Thanks.
Edit :
Here are my routes :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Solution 1:[1]
the first argument is interpretted as the Action
@url.Action("myaction","myController")
Solution 2:[2]
The signature of Url.Action is:
Url.Action(string actionName, string controllerName)
According to your code, the order of your parameters are incorrect, you should try:
@Url.Action("myAction", "myController")
Also remember that to remove the "Controller" part of the controller, for exemple, if I have a CustomerController with a Index action that would be like that:
@Url.Action("Index", "Customer")
Solution 3:[3]
I've had same problem... My solution:
@Html.ActionLink("Link text", "myAction", "myController", new { id=item.ID }, null)
Just adding null to last parameter.
Solution 4:[4]
For default action ( action that is not defined in route ), normal HTTP GET will automagically go to, you just need to use null action.
@Url.Action( null, "Customer" )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rafay |
| Solution 2 | Charles Ouellet |
| Solution 3 | Tomino |
| Solution 4 | KrittMasta |
