+ (plus) sign in Web API routing

2019-03-12 10:40发布

I'm working with an asp.net web api project, and I have to pass an mobile number through a post. But i cannot return a plus sign.

my route:

config.Routes.MapHttpRoute( 
    name: "SmsRoute",
    routeTemplate: "rest/sms/{country}/{company}/phone/{mobilenumber}",
    defaults: new { controller = "Sms", action = "PostSms" });

controller:

public HttpResponseMessage PostSms(string country, string company, string mobilenumber)
{
    return Request.CreateResponse( HttpStatusCode.Created );
}

When I run this post method on fiddler:

http://index.html/rest/sms/se/company/phone/46700101010/messages/out

I'm getting this response:

enter image description here

But I want it to be able to show me the plus sign.

3条回答
对你真心纯属浪费
2楼-- · 2019-03-12 11:25

This a common anomaly that we face while adding parameters in the url's. Here you can have a hint of what your OP is and what you might need at a later run

You can have many options as for + you can encode in your java script file using

encodeURIComponent();

And also for your ease if you want to add any special characters in your url like a . then simply set the property relaxedUrlToFileSystemMapping in your web.config

<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

Or you can just add or remove some of the characters to block/allow using requestPathInvalidCharacters in your web.config

<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,:,\,?"/>
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-12 11:47

IIS prevents plus-signs from being in URL path elements.

To disable this feature, in web.config:

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="true" />
    </security>
</system.webServer>
查看更多
登录 后发表回答