Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API 2 requires trailing slash for custom attribute routing to work

I have created a Web API 2 project and although the APIs work fine, I must enter a trailing slash for them to do so.

This results in a 404

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

This shows 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 this has one parameter that is an integer...

It seems to be something to do with the decimal type as if I make a slight variation in the URL and use a parameter, the API returns the results without issue:

This variation 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 generate my API documentation and that automatically uses the first of the above URLs and includes built-in testing which, of course, fails. That's not so good for any developers who are referencing the API docs.

Can anyone explain why this may be happening and how I get it to work without the trailing slash?

Route Config

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;
    }
like image 335
ChrisCurrie Avatar asked Jun 09 '15 17:06

ChrisCurrie


People also ask

What is routing in web API 2?

"Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

How do I use the route attribute with web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config.MapHttpAttributeRoutes () method. Consider the following example of attribute routing.

What is the difference between Uri routing and attribute routing?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes.

What is routing in ASP NET?

An excerpt from asp.net about routing, "Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.


2 Answers

Use runAllManagedModulesForAllRequests. Without it IIS thinks it is a file request with extension as number part after decimal point. File is not found, 404.

like image 75
ranquild Avatar answered Sep 30 '22 08:09

ranquild


You can use the below code in your web.cong file:

<system.webServer>
 <modules runAllManagedModulesForAllRequests="true">
 </modules>
</system.webServer>
like image 37
Papun Sahoo Avatar answered Sep 30 '22 08:09

Papun Sahoo