Mark
Mark

Reputation: 404

Routes and Url helpers confusion

I'm a little confused on the MVC front for this reason, I have the following default route defined;

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

When I use a Url helper, i.e.

@Url.Action("MyAction")

it generates this url;

/?action=MyAction&controller=MyController

and never finds my action method. How are the Urls generated by the helpers and how do I correct this?

Upvotes: 1

Views: 473

Answers (2)

reven
reven

Reputation: 41

I had the same issue, I had a web app that was built used WebForms and slowly migrating parts to MVC, to support both I had a route entry which ended up breaking the routing evaluation code and caused the funny action urls

this blog post fixed my issue

Upvotes: 0

fearofawhackplanet
fearofawhackplanet

Reputation: 53396

Just use the overload to specify the action and controller:

@Url.Action("MyAction", "MyController")

If you use the overload which only takes the action name, the controller is taken from the current route data. Default routing doesn't come into it.

i.e.

@Url.Action("MyAction")

is equivalent to

@Url.Action("MyAction", (string)ViewContext.RouteData.Values["controller"])

Upvotes: 1

Related Questions