1

I need to pass a query string to a redirect. I know you can do this like so:

return RedirectToAction("Index", new { param1 = "hello" });

will go to /Index?param1=hello.

But I need to pass a parameter which has a hyphen in the name. Let's call it "data-param". Since hyphens aren't allowed in C# names, I can't do this in the above way. I know in some places in MVC, underscores are used to handle this, but that doesn't work here either, the underscore is passed to the query string as-is.

So my question is, how do I redirect to /Index?data-param=hello?

MarredCheese
  • 17,541
  • 8
  • 92
  • 91
user3592246
  • 157
  • 2
  • 16
  • 1
    When you say you _need_ to pass a param with a hyphenated name, is that a critical requirement? Sometimes the easiest solution is to go with the flow. However, I don't think this has changed much since version 2, so you could look at: http://stackoverflow.com/questions/2782156/handling-mvc2-variables-with-hyphens-in-their-name – glenatron May 12 '14 at 16:40
  • Unfortunately it's for a 3rd party library, so yes I do need to pass it hyphenated. – user3592246 May 12 '14 at 16:41
  • I did come across the linked question when searching for help on this topic, but it doesn't seem to be quite what I'm looking for here. I am (probably quite obviously) very new to MVC and ASP so I could be missing something. What I mean is the linked question is about receiving parameters from a 3rd party library that contain hyphens, but I want the opposite - to send the parameters to the 3rd party library. – user3592246 May 12 '14 at 16:44

2 Answers2

2

If you are trying to build a url, you can always slightly bypass the MVC routing and just pass the complete url in the old-fashioned way.

string dataParam="hello";
int otherParam=5;
return Redirect( String.Format("/Index?data-param={0}&data-param2={1}", dataParam, otherParam) );

If you are going outside of your own MVC application then you may not want RedirectToAction anyway unless you are redirecting to an action.

glenatron
  • 11,018
  • 13
  • 64
  • 112
  • The Index controller is part of the MVC application, but on the view it renders is a component using a 3rd party library, which takes the data it needs from the query string. As I'm new to MVC I'm not entirely sure of the consequences of using Redirect instead of RedirectToAction but it seems to work in this case, so thanks! – user3592246 May 12 '14 at 17:09
  • Basically RedirectToAction will manage a bunch of stuff to do with finding your application root and tidying up your path if necessary. It's convenient to use where you can, but as long as you are conscious you need to keep an eye on pathing ( for example when you are running in a virtual directory rather than site root ) then there is no harm in bypassing it- the URLs should work either way. – glenatron May 12 '14 at 17:11
  • Looks like a slightly better way to achieve this is to use Url.Action("Index") to get the URL for the Index controller, rather than hardcoding it in the string, and use Redirect as above. – user3592246 May 14 '14 at 11:10
  • Yes, _but_ only if you are redirecting within your own application. That Action routing will only find actions it knows about, so if you wanted to send data to your friend's PHP application on another server, you would be out of luck. – glenatron May 14 '14 at 13:57
0

This works (I am using ASP.NET Core 3.1 and can't speak to earlier versions):

var params = new Dictionary<string, string>()
{
    { "data-param1", "hello" }
    { "data-param1", "2" }
};
return RedirectToAction("Index", params);

You can use Dictionary<string, object>() instead if you want, though you'll give up some control (e.g. false gets translated to "False" in the URL instead of "false"). And you should use nameof() for better maintainability:

var params = new Dictionary<string, object>()
{
    { "data-param1", "hello" }
    { "data-param2", 2 }
};
return RedirectToAction(nameof(Index), params);

Also note that this dictionary technique works with other methods too like RedirectToRoute().

MarredCheese
  • 17,541
  • 8
  • 92
  • 91