Search code examples
c#asp.net-web-apiasp.net-mvc-routing

The constraint entry 'inboundHttpMethod' must have a string value or be of a type which implements 'IHttpRouteConstraint


I am trying to implement attribute routing, but getting following error.

The constraint entry 'inboundHttpMethod' on the route with route template 'authenticate' must have a string value or be of a type which implements 'IHttpRouteConstraint

I already added the line of code in Global.asax,

    AttributeRoutingHttpConfig.RegisterRoutes(GlobalConfiguration.Configuration.Routes);

and the below code in AttributeRoutingHttpConfig.cs,

routes.MapHttpAttributeRoutes(cfig =>
            {
                cfig.UseLowercaseRoutes = true;
                cfig.AutoGenerateRouteNames = true;
                cfig.AddRoutesFromAssemblyOf<AuthenticateController>();
                cfig.InMemory = true;
            });

Anyone know about the issue, actually I am new to c#.


Solution

  • You are using the wrong packages.

    From what I can see you are trying to implement attribute routing using this NuGet package (old and no longer supported).

    This package supports Web API v1 (assembly version 4.*), and not Web API 2 (assembly version 5.*).

    Web API 2 support for attribute routing is native. This tutorial may help you implementing such a feature: Attribute Routing in Web API 2

    Here it is a small example:

    [RoutePrefix("v1/myexample")]
    public MyController : ApiController {
        [Route("foo")]
        public string GetFoo()
        {
            return "foo";
        }
    }
    

    This action may be reached at the following endpoint: http://myhost/v1/myexample/foo.

    Remember to register them in your WebApiConfig.cs file:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
    
            // Other Web API configuration
        }
    }