ASP.Net MVC: Passing a string parameter to an acti

2019-06-16 07:06发布

问题:

I would like to know how to pass a string parameter using RedirectToAction().

Let's say I have this route:

routes.MapRoute(
  "MyRoute",
  "SomeController/SomeAction/{id}/{MyString}",
  new { controller = "SomeController", action = "SomeAction", id = 0, MyString = UrlParameter.Optional }
);

And in SomeController, I have an action doing a redirection as follows:

return RedirectToAction( "SomeAction", new { id = 23, MyString = someString } );

I tried this redirection with someString = "!@#$%?&* 1" and it always fails, no matter if I encode the string. I tried encoding it with HttpUtility.UrlEncode(someString), HttpUtility.UrlPathEncode(someString), and with Uri.EscapeUriString(someString) to no avail.

So I resorted to us TempData to pass someString, but still, I would be curious to know how to make the code above work, just to satisfy my curiosity.

回答1:

I think the issue might be either in your route order, or in your controller. Here is some code that I got to work.

Route Definitions

        routes.MapRoute(
            "TestRoute",
            "Home/Testing/{id}/{MyString}",
            new { controller = "Home", action = "Testing", id = 0, MyString = UrlParameter.Optional }
        );

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

// note how the TestRoute comes before the Default route

Controller Action Methods

    public ActionResult MoreTesting()
    {
        return RedirectToAction("Testing", new { id = 23, MyString = "Hello" });
    }

    public string Testing(int id, string MyString)
    {
        return id.ToString() + MyString;
    }

When I browse to /Home/MoreTesting I get the desired output of "23Hello" to output in my browser. Can you post your routes and your Controller code?



回答2:

OK, I know this question is a few days old but I wasn't sure if you got this issue sorted or not so I had a look. I played around with this for a while now and this is what the problem is and how you could solve it.

The problem you are having is that the special characters causing issues are one of the many (I think 20) special characters, such as % and ".

In your example the problem is the % character. As pointed out by Priyank here:

The route values are posted as part of the URL string.

The Url string (not query string parameter) can't handle %(%25), "(%22) and so on. Further, as pointed out by Lee Gunn in the same post: http://localhost:1423/Home/Testing/23/!%40%23%24%25%3f%26*%201 - (this will blow up)

One of the ways to fix this is to remove {MyString} from the route mapping. To make your root mapping look like this:

routes.MapRoute(
    "TestRoute",
    "Home/Testing/{id}",
    new { controller = "Home", action = "Testing", id = 0, MyString = UrlParameter.Optional }
);

This will cause the post to generate this:

http://localhost:1423/Home/Testing/23?MyString=!%2540%2523%2524%2525%2B1

Now when you set MyString it will be turned into a query string parameter which works perfectly fine. I did try that and it did work.

Priyank also mentioned in the SO post I linked above that you maybe could solve this with a custom ValueProvider but you would have to follow his linked article their to check if that is something that would apply to you.