繁体   English   中英

带有电子邮件地址参数的WebApi方法从HttpClient返回404

[英]WebApi method with email address parameter returns 404 from HttpClient

我有一个WebApi控制器,其方法如下:

[HttpGet]
[AcceptVerbs("GET")]
public HttpResponseMessage Run(string reportName, int someId, string someText, DateTime asOfDate, string username)

我有专门为此操作配置的自定义路由。 当我将浏览器导航到Web服务时,一切正常,并执行相应的操作:

http://localhost:xxxx/ControllerName/Run/asdf/1/asdf/07-01-2012/user@domain.com

但是,当我尝试使用HttpClient以编程方式调用Web服务并执行“获取”时,我收到404错误。 当username参数不是电子邮件地址时,我不会收到错误。 例如,当用户名只是“用户”时,一切正常。 以下是示例代码:

var url = "http://localhost:xxxx/ControllerName/Run/asdf/1/asdf/07-01-2012/user@domain.com"
var client = new System.Net.Http.HttpClient();
var response = client.Get(url);

//fails here with 404 error
response.EnsureSuccessStatusCode();

我试过UrlEncoding电子邮件地址没有运气。 任何建议表示赞赏。

只是一个快速思考......“。com”可能导致问题吗?

这是因为IIS正在尝试映射特殊字符。 将以下内容添加到web.config应解决此问题:

<system.webServer>  
     <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

更多信息请访问: http//www.iis.net/configreference/system.webserver/modules

将以下内容添加到web.config不应该解决完整的问题:

<system.webServer>  
     <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

该解决方案不再适用于uri中的额外路径段。 https://localhost:xxxx/user@domain.com确实有效

https://localhost:xxxx/path/user@domain.com不起作用

我四处走动,我明白这是一个扩展。 (.cs给出了与.com不同的错误)并最终找到了这个: ASP.net MVC4 WebApi路由,其中​​包含文件名

我的解决方案是在<system.webServer><handlers>部分添加或更改以下处理<handlers>

<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" 
           path="*.*" 
           verb="*" 
           type="System.Web.Handlers.TransferRequestHandler" 
           preCondition="integratedMode,runtimeVersionv4.0" />

或将路径更改为path=*@*.*

您需要一个基本的URL查询。

[Route("api/emails")]
public HttpResponseMessage Run(string email) {...}

GET api/emails?email=user@domain.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM