Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.Action with RouteValueDictionary with Protocol

Tags:

asp.net-mvc

been looking at this for a while, and feel like I am just being stupid want to get some more eyes on it..

I need to generate a full URL, (e.g. http://www.domain.com/controller/action?a=1&b=2), generally I just use Url.Action to do this with no problems by specifying the protocol:

var url = Url.Action("Action", "Controller", new { a = 1, b = 2 }, "http");

I've started putting together a class that returns a RouteValueDictionary to make these anonymous objects disappear. However, I can't get it to work alongside the helper.

var x = Url.Action("Action", "Controller", new RouteValueDictionary(new { a = 1, b = 2 }), "http");
// "http://127.0.0.1/Controller/Action?Count=2&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D",

var y = Url.Action("Action", "Controller", new { a = 1, b = 2 }, "http");
// "http://127.0.0.1/Controller/Action?a=1&b=2"

Any pointers that lead to a facepalm greatly appreciated :)

Update:

It's probably best to clarify that in the above sample, I need to get the 'X' variable working correctly, since the RouteValueDictionary is created elsewhere in code. Assume that the RouteValueDictionary is correct.

I just don't understand why this works with anonymous object, but the same object wrapped in the same object wrapped in a RouteValueDictionary makes the helper freak out.

like image 374
Rob Cooper Avatar asked Mar 15 '13 13:03

Rob Cooper


People also ask

What is action method in url?

Action() Generates a string to a fully qualified URL to an action method. Action(String) Generates a fully qualified URL to an action method by using the specified action name. Action(String, Object)

What is the difference between URL action () and HTML action ()?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is RouteValueDictionary?

The RouteValueDictionary class enables you to work with collections of key/value pairs that are required in order to use ASP.NET routing. You use the RouteValueDictionary object to define values for the Constraints, DataTokens, and Defaults properties.


2 Answers

Interestingly, it looks like your specific example is matching a method signature that takes "object" as a property, rather than RouteValueDictionary. As such, it's just ToString()ing out the typename, rather than correctly serializing a RouteValueDictionary

 var x = Url.Action("Index", "Home", new RouteValueDictionary(new { a = 1, b = 2 }), "http", string.Empty);

Note "string.Empty" at the end.

That's enough to force the code to use a different overload, that accepts a RouteValueDictionary and as such, serializes correctly.

// http://localhost:55110/?a=1&b=2
like image 146
DavidWhitney Avatar answered Sep 18 '22 12:09

DavidWhitney


The overload that you are using expects type "object" for the parameter where you are passing the RouteValueDictionary. For some reason this is causing the issue, maybe something to do with .ToString()? Use an overload that accepts a RouteValueDictionary and this should work.

To test this, add a hostName argument to select the overload shown below:

Correct overload

EDIT

You could use this extension in your project to add the overload that you require to Url.Action. Internally it will resolve and add the hostName from the request.

public static string Action
    (this UrlHelper helper, string action, 
     string controller, RouteValueDictionary routeValues, string protocol)
{
     string hostName = helper.RequestContext.HttpContext.Request.Url.Host;
     return helper.Action(action, controller, routeValues, protocol, hostName);
}
like image 20
Oliver Avatar answered Sep 18 '22 12:09

Oliver