Matt Wolin
Matt Wolin

Reputation: 701

WebApi method with email address parameter returns 404 from HttpClient

I have a WebApi controller with a method that looks like such:

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

I have a custom route configured specifically for this action. When I navigate my browser to the web service, everything works fine, and the appropriate action is executed:

http://localhost:xxxx/ControllerName/Run/asdf/1/asdf/07-01-2012/[email protected]

However, when I attempt to programatically call the web service using the HttpClient and executing a "Get" I get a 404 error. I don't get an error when the username parameter is not an email address. For example, when the username is just "user" everything works fine. Here is the sample code:

var url = "http://localhost:xxxx/ControllerName/Run/asdf/1/asdf/07-01-2012/[email protected]"
var client = new System.Net.Http.HttpClient();
var response = client.Get(url);

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

I have tried UrlEncoding the email address with no luck. Any advice is appreciated.

Upvotes: 35

Views: 19571

Answers (4)

Roberto B
Roberto B

Reputation: 582

Adding the following to the web.config should not fix the complete issue:

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

The solution no longer works as an extra path segment is used in the uri for me. https://localhost:xxxx/[email protected] does work

https://localhost:xxxx/path/[email protected] does not work

I google around and around and i understood that it is an extension. (.cs gives a different error as .com) and finaly find this: ASP.net MVC4 WebApi route with file-name in it

My solution was to add or change the following handler in the <handlers> section of your <system.webServer>:

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

or change the path to path=*@*.*

Upvotes: 1

Sittipol Pungprakiet
Sittipol Pungprakiet

Reputation: 21

You need a basic URL query.

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

GET api/[email protected]

Upvotes: 1

DataPispor
DataPispor

Reputation: 381

This is because IIS is trying to map special characters. Adding the following to the web.config should fix the issue:

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

More information here: http://www.iis.net/configreference/system.webserver/modules

Upvotes: 22

Ryan Ferretti
Ryan Ferretti

Reputation: 2961

Just a quick thought... could the ".com" be causing the issue?

Upvotes: 40

Related Questions