1

I am trying to receive a parameter by URL, but if this parameter has a '+' I do not receive it, for example:

www.domain.com/api/controller/Demo?guid=123+456

[HttpPost("Demo")]
public IActionResult PostData([FromQuery]string guid, Model model)
{

Inside the function, the guid value is 123 456

vidgarga
  • 320
  • 1
  • 3
  • 8

1 Answers1

2

if this parameter has a '+' I do not receive it, for example: www.domain.com/api/controller/Demo?guid=123+456. Inside the function, the guid value is 123 456

To achieve your requirement, you can try following approaches:

Approach 1: manually replace the + sign to %2B while client make request to your API action.

Approach 2: encode query string guid=123+456 by using URL Rewriting Middleware, like below.

In Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //...

    using (StreamReader iisUrlRewriteStreamReader =
            File.OpenText("IISUrlRewrite.xml"))
    {
        var options = new RewriteOptions()
            .AddIISUrlRewrite(iisUrlRewriteStreamReader);

        app.UseRewriter(options);
    }

    //...
}

In IISUrlRewrite.xml

<?xml version="1.0" encoding="utf-8" ?>
<rewrite>
  <rules>
      <rule name="TESTURL">
            <match url="api/your_controller_name/Demo" />
            <conditions>
                <add input="{QUERY_STRING}" pattern="guid=([0-9]+\+[0-9]+)" />
            </conditions>
            <action type="Rewrite" url="api/your_controller_name/Demo?guid={UrlEncode:{C:1}}" appendQueryString="false" />
        </rule> 
  </rules>
</rewrite>

Test Result

enter image description here

Fei Han
  • 26,415
  • 1
  • 30
  • 41