ApiController returns 404 when ID contains period

asp.net Mvcasp.net Web-Apiasp.net Mvc-Routing

asp.net Mvc Problem Overview


I have an ApiController and I want to use email addresses as the ID parameter for requests:

// GET api/employees/[email protected]
public CompactEmployee Get(string id) {
   var email = id;
   return GetEmployeeByEmail(email);
}

However, I cannot get this to work (returns 404):

http://localhost:1080/api/employees/[email protected]

The following all work:

  • http://localhost:1080/api/employees/employee@company
  • http://localhost:1080/api/employees/employee@company.
  • http://localhost:1080/api/[email protected]

I have set relaxedUrlToFileSystemMapping="true" in my web.config as detailed by Phil Haack.

I would very much love the full email address to work, but any time the period is followed by any other character, the request returns a 404. Any help would be greatly appreciated!

Solution

Due to a lack of other options, I've headed in the direction Maggie suggested and used the answer from this question to create a rewrite rule to automatically append a trailing slash when I need an email in the URL.

<system.webServer>
  ....   
  <rewrite>
    <rules>
      <rule name="Add trailing slash" stopProcessing="true">
        <match url="^(api/employees/.*\.[a-z]{2,4})$" />
        <action type="Rewrite" url="{R:1}/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Would adding a trailing slash work for your scenario?

http://localhost:33021/api/employees/employee@company.com/

Solution 2 - asp.net Mvc

Check your IIS settings:

Home Directory -> Configuration

Edit the .aspx application extension and ensure that the setting Verify that file exists is off.

UPDATE

I've just tested with a default MVC4 Web API project

URL: http://localhost:10983/api/values/[email protected]

Action in ValuesController:

public string Get(string id)
{
    return id;
}

This was the response:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cool@email.com</string>

Solution 3 - asp.net Mvc

This is what worked for me:

I was running on targetFramework = 4.6.1. I have upgraded to 4.6.2 and added this in web.config:

<system.web>
		<customErrors mode="Off"/>
		<compilation debug="true" targetFramework="4.6.2"/>
		<!-- This will allow to search for stuff that contains . & etc.-->
		<httpRuntime targetFramework="4.6.2" maxRequestLength="100000" maxUrlLength="2048" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters=""/>
  </system.web>

The requestPathInvalidCharacters="" is to be able to have stuff like & etc in URI, in encoded form, of course.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan FreelandView Question on Stackoverflow
Solution 1 - asp.net MvcMaggie YingView Answer on Stackoverflow
Solution 2 - asp.net MvcMightymukeView Answer on Stackoverflow
Solution 3 - asp.net MvcNightrainView Answer on Stackoverflow