Search code examples
asp.net-mvcasp.net-web-apiasp.net-mvc-routing

ApiController returns 404 when ID contains period


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

// GET api/employees/email@address.com
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/employee@company.com

The following all work:

  • http://localhost:1080/api/employees/employee@company
  • http://localhost:1080/api/employees/employee@company.
  • http://localhost:1080/api/employees?id=employee@company.com

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>

Solution

  • Would adding a trailing slash work for your scenario?

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