Web API 2 requires the trailing slash for user attribute routing to work

I created a Web API 2 project, and although the APIs are working fine, I have to make a trailing slash for them.

The result is 404

http://www.myURL.com/api/v1/get/addressfromlatlong/UK/50.9742794/-0.1146699 

Here's the JSON response as intended

 http://www.myURL.com/api/v1/get/addressfromlatlong/UK/50.9742794/-0.1146699/ 

I have another controller with a custom action that works fine. The only difference is that it has one parameter, which is an integer ...

Something seems to be related to the decimal type, as if I are making a small change in the URL and using the parameter, the API returns the results without problems:

This option also shows the JSON response as intended

 http://www.myURL.com/api/v1/get/addressfromlatlong/UK/50.9742794/?longitude=-0.1146699 

It's not the end of the world, but I also use Swagger to create API documentation and automatically use the first of the above URLs and include built-in testing, which of course fails. This is not so good for developers who reference API documents.

Can someone explain why this can happen and how can I make it work without a trailing slash?

Route configuration

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } 

Custom Attributes and Controller Action

 [Route("get/addressfromlatlong/UK/{latitude:decimal=0}/{longitude:decimal=0}")] public AddressDetails GetAddressDetailsByLatLong(decimal latitude, decimal longitude) { AddressDetails addressDetails = repository.GetAddressDetailsByLatLong(latitude, longitude); return addressDetails; } 
+5
source share
1 answer

Use runAllManagedModulesForAllRequests . Without it, IIS considers it to be a file with the extension as the numeric part after the decimal point. File not found, 404.

+4
source

All Articles