Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlHelper.Action includes undesired additional parameters

I have a method in the controller ApplicationsController, in which I need to get the base URL for an action method:

public ActionResult MyAction(string id)
{
    var url = Url.Action("MyAction", "Applications");
    ...
}

The problem is that this includes the string id from the current route data, when I need the URL without (the URL is used to fetch content from a CMS on a URL-based lookup).

I have tried passing null and new { } as the routeValues parameter to no avail.

The matching route is as follows (above all other routes):

routes.MapLowercaseRoute(
    name: "Applications",
    url: "applications/{action}/{id}",
    defaults: new { controller = "Applications",
                    action = "Index", id = UrlParameter.Optional });

I've seen a couple of other questions touch on this but none of them seem to have a viable solution. At present, I am resorting to hardcoding the path in the controller; however, I'd like to be able to abstract this into an action filter, so I need to be able to generate the URL.

Is there a clean/conventional way to prevent this behaviour?

like image 887
Ant P Avatar asked Dec 03 '13 11:12

Ant P


People also ask

What does URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.

How do you pass dynamic values in URL action?

You can concat the client-side variables with the server-side url generated by this method, which is a string on the output. Try something like this: var firstname = "abc"; var username = "abcd"; location. href = '@Url.

What is URL RouteUrl?

RouteUrl(String, Object) Generates a fully qualified URL for the specified route values by using a route name. RouteUrl(String, RouteValueDictionary) Generates a fully qualified URL for the specified route values by using a route name.

What is URL content in MVC?

Url.Content is used when you wish to resolve a URL for any file or resource on your site and you would pass it the relative path: @Url.Content("~/path/file.htm") Url.Action is used to resolve an action from a controller such as: @Url.Action("ActionName", "ControllerName", new { variable = value })


1 Answers

Ok, I see the problem. It's something called "Segment variable reuse". When generating the routes for outbound URLs, and trying to find values for each of the segment variables in a route’s URL pattern, the routing system will look at the values from the current request. This is a behavior that confuses many programmers and can lead to a lengthy debugging session. The routing system is keen to make a match against a route, to the extent that it will reuse segment variable values from the incoming URL. So I think you have to override the value like Julien suggested :

var url = Url.Action("MyAction", "Applications", new { id = "" })
like image 195
Tomasz Jaskuλa Avatar answered Nov 11 '22 14:11

Tomasz Jaskuλa