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/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cebbbdabbc8eaaa1a3afa7a0e0ada1a3">[email protected]</a>

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/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47323422350723282a262e296924282a">[email protected]</a>"
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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just a quick thought… could the “.com” be causing the issue?

Method 2

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

Method 3

You need a basic URL query.

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

GET api/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096c646860657a366c64686065347c7a6c7b496d6664686067276a6664">[email protected]</a>

Method 4

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=*@*.*


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x