C# +;(plus)登录Web API路由
我正在处理一个asp.NETWebAPI项目,我必须通过帖子传递一个手机号码。但我不能返回加号 我的路线:C# +;(plus)登录Web API路由,c#,asp.net-web-api,asp.net-web-api-routing,C#,Asp.net Web Api,Asp.net Web Api Routing,我正在处理一个asp.NETWebAPI项目,我必须通过帖子传递一个手机号码。但我不能返回加号 我的路线: config.Routes.MapHttpRoute( name: "SmsRoute", routeTemplate: "rest/sms/{country}/{company}/phone/{mobilenumber}", defaults: new { controller = "Sms", action = "PostSms" }); 控制器: publ
config.Routes.MapHttpRoute(
name: "SmsRoute",
routeTemplate: "rest/sms/{country}/{company}/phone/{mobilenumber}",
defaults: new { controller = "Sms", action = "PostSms" });
控制器:
public HttpResponseMessage PostSms(string country, string company, string mobilenumber)
{
return Request.CreateResponse( HttpStatusCode.Created );
}
当我在fiddler上运行此post方法时:
http://index.html/rest/sms/se/company/phone/46700101010/messages/out
我得到的回应是:
但我希望它能显示加号。对
+
进行编码:
将您的feedle输入替换为以下输入:
另见:
这是我们在url中添加参数时遇到的常见异常。在这里,您可以了解您的OP是什么,以及在以后的运行中可能需要什么 对于
+
,您可以使用
encodeURIComponent();
为了方便起见,如果您想在url中添加任何特殊字符,如
,只需在web.config中设置属性relaxedUrlToFileSystemMapping
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
IIS阻止加号出现在URL路径元素中 要禁用此功能,请在
web.config
中:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
谢谢您的提示,但它不是编码使用给定示例时会发生什么?编码在查询字符串中有效,但在URL路径组件中无效。这是一个WebAPI问题。relaxedUrlToFileSystemMapping
禁止的字符列表是,:,", /, \, |, ? 和*
。requestPathInvalidCharacters
的默认值是,*,%,&,:,\,?
。据我所知,这两个都对加号没有任何影响。
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>