Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API 2 routing - Route attribute

Question is regarding defining custom routes with the Route attribute.

I know that in the WebApiConfig class you always define the default route,

configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
  new { id = RouteParameter.Optional });

What I cannot get working is when I want to pass another parameter. I know I can do this (code below is defined underneath the default route listed above):

    //configuration.Routes.MapHttpRoute(
    //    name: "GetBrandImagePaths",
    //    routeTemplate: "api/{controller}/{id}/{type}");

But I'd rather, instead of defining all these routes in the WebApiConfig file, use custom routing. However, if I do not have the commented out code above in the file, I get a 404. There leading me to believe the custom Route is not even being looked at.

public class HelperApiController : ApiController
{
    [HttpGet]
    [Route("api/helperapi/{id}/{type}")]
    public string GetBrandImages(int id, string type)
    {
        .....
    }
}

How can I have it so I can use routes defined in the WebApiConfig file, AND defining custom routes inside individual API controllers.

Note that this project is also a MVC project (not just WebApi). Is there something I'm missing, doing incorrectly etc? I know there's numerous posts out there defining how to pass multiple params, but I think my question is a little more specific on to why one works and not the other.

like image 504
Rob Scott Avatar asked Nov 10 '16 16:11

Rob Scott


People also ask

What is ApiController attribute?

The [ApiController] attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.

In what type of routing we need to specify the route attribute in the action method?

Attribute routing should configure before the convention-based routing. When you combine attribute routing with convention-based routing, actions which do not have Route attribute for defining attribute-based routing will work according to convention-based routing.

What is attribute-based routing?

Attribute-Based Routing is a process used by contact center applications to distribute tasks according to a set of defined characteristics.


2 Answers

You need to call config.MapHttpAttributeRoutes().

This will parse all the Controller classes and derive the routes from the attributes.

I would not mix this with the standard routing.

like image 89
toadflakz Avatar answered Oct 27 '22 01:10

toadflakz


Attribute Routing in ASP.NET Web API 2

Enabling Attribute Routing

To enable attribute routing, call MapHttpAttributeRoutes during configuration. This extension method is defined in the System.Web.Http.HttpConfigurationExtensions class.

using System.Web.Http;

namespace WebApplication
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            // Other Web API configuration not shown.
        }
    }
}

Attribute routing can be combined with convention-based routing. To define convention-based routes, call the MapHttpRoute method.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
like image 27
Nkosi Avatar answered Oct 26 '22 23:10

Nkosi