1
votes

Is there a way in ASP.NET MVC to generate a url with an Html.helper without specifying the action (and thus routing it to the default action)?

Basically i want a url that outputs {lang}/controller, i.e.: ~/EN/Home

But every html-helper in MVC seems to demand to specify a specific controller action, even

<a href="@Url.Action(null, @Resources.Home">Click here</a>

outputs EN/Home/PartialViewName instead of just EN/Home.... (I am generating it on a partial view)

UPDATE 1:

apparently, the "Index" action doesn't show up in the generated url, ie:

@Html.ActionLink("Click here", "Index", @Resources.Home)

outputs /EN/Home

but if you want to add parameters, the problem remains the same unfortunately. Non of the following output the correct url (ie EN/Home/3)

@Html.ActionLink("Click here", "Index", @Resources.Home, new { id = home.Id }, null)

<a href="@Url.Action(null, @Resources.Home)/@home.Id">Click here</a>

UPDATE 2:

Solved it for now like this:

@Html.ActionLink("Click here", "Index", string.Format(Resources.Home + "/" + home.Id))

but that can't be the only/correct solution for this problem?

2

2 Answers

1
votes

How about adding a route for home/{id} like the following?

        routes.MapRoute(
            name: "HomeID",
            url: "home/{id}",
            defaults: new { controller = "Home", action = "Index", id = 0 });

Then when you create the URL it will give you /home/32 (if generated with ID of 32)

0
votes

How about creating your own helper that will generate the URL you want. It can even be a simple call to the mvc implementation of URL.Action and then replacing the action name with what you want.