ApiController returns 404 when the identifier contains a period

I have an ApiController and I want to use email addresses as an 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 works:

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

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

I would really like the full email address for the job, but at any moment followed by some other character, the request returns 404. Any help would be greatly appreciated!

Decision

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

 <system.webServer> .... <rewrite> <rules> <rule name="Add trailing slash" stopProcessing="true"> <match url="^(api/employees/.*\.[az]{2,4})$" /> <action type="Rewrite" url="{R:1}/" /> </rule> </rules> </rewrite> </system.webServer> 
+49
asp.net-mvc asp.net-web-api asp.net-mvc-routing
Nov 08
source share
3 answers

Will slash work be added for your script?

 http://localhost:33021/api/employees/employee@company.com/ 
+69
Nov 08 '12 at 22:33
source share

Check your IIS settings:

Home Directory -> Configuration

Edit the .aspx application extension and verify that the Verify that file exists option is turned off.

UPDATE

I just tested the MVC4 project by default web interface

URL: http://localhost:10983/api/values/cool@email.com

Action in ValuesController :

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

That was the answer:

 <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cool@email.com</string> 
+2
Nov 08
source share

This is what worked for me:

I worked on targetFramework = 4.6.1. I upgraded to 4.6.2 and added this to 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> 

requestPathInvalidCharacters="" should be able, for example, to have requestPathInvalidCharacters="" like & etc in the URI, in encoded form.

0
Sep 11 '18 at 23:45
source share



All Articles