WebApi method with email address parameter returns 404 from HttpClient

18,567

Solution 1

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

Solution 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

Solution 3

You need a basic URL query.

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

GET api/[email protected]

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

Share:
18,567
Matt Wolin
Author by

Matt Wolin

Updated on August 06, 2022

Comments

  • Matt Wolin
    Matt Wolin almost 2 years

    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.