2

I am using ASP.NET Versioning Library, I have followed steps to add this library to a very basic ASP.NET Web API 2 project, Here are the contents of my files: Global.asax file:

 public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        }
    }

Web API Config:

  public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.AddApiVersioning(c => c.AssumeDefaultVersionWhenUnspecified = true);   
        }
    }

First controller:

namespace WebApplication5.Controllers
{
    [ApiVersion("1.0")]
    [RoutePrefix("api1")]
    public class ValuesController : ApiController
    {
        [Route("values")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        [Route("values/{id}")]
        public string Get(int id)
        {
            return "value";
        }
    }
}

Second controller:

namespace WebApplication5.Controllers
{
    [ApiVersion("2.0")]
    [RoutePrefix("api1")]
    [ControllerName("Values")]
    public class ValuesController2 : ApiController
    {
        [Route("values")]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        [Route("values/{id}")]

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }
    }
}

When I run the application and navigate to this url: http://localhost:5428/api1/values?api-version=1.0 I get the desired results, But When I navigate to this url: http://localhost:5428/api1/values?api-version=2.0 I encounter an error message stating that No route providing a controller name with API version '2.0' was found to match request URI 'http://localhost:5428/api1/values', I was wondering what am I missing in this basic setup?

If I remove [ControllerName] attribute from ValuesController2 nothing changes and the issue persists.

I tried to change the namespace of the second controller, But It didn't solve the problem either !

I am using version 3.0.1 of this library.

Rathma
  • 1,196
  • 2
  • 33
  • 65

2 Answers2

3

Just realised what your issue is, the controller name should be Values2Controller not ValuesController2.

The controller is located using be convention on the name and it drops the Controller part but yours is Controller2 so I would guess it's not finding it as per the message you got back.

Andrew HB
  • 332
  • 1
  • 3
  • 11
  • Wow ! It really worked, That was a very subtle point that I missed. – Rathma Jun 16 '19 at 05:15
  • 1
    I don't want to take anything away from Andrew HB, but I did want to further elaborate why this happens. This is not due to API Versioning, but rather how ASP.NET Web API works. If you examine the [DefaultHttpControllerTypeResolver.IsControllerType](https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Http/Dispatcher/DefaultHttpControllerTypeResolver.cs#L49) method, you'll see that a valid controller type **must** end with `Controller`, which your controller does not. As I recall, the history of that is to ignore non-abstract super types such as `XXXControllerBase`. – Chris Martinez Jun 18 '19 at 20:44
0

Not sure on the validity of having the ApiVersion attribute on the controller, but on my implementation I have that attribute on my action e.g.

    [AllowAnonymous]
    [Route("[controller]")]
    [ApiController]
    public class VersionDemoController
    {
        [ApiVersion("1.1")]
        [HttpGet("requestStuff")]
        public string RequestStuff()
        {
            return "Stuff using Version 1.1 API";
        }
    }

Update: Just tried using the attribute on the controller and it seems to work fine, my namespaces are different with the action names the same. I am using version 3.1.2 of the versioning package though.

Andrew HB
  • 332
  • 1
  • 3
  • 11
  • I guess you are working with .net core version of this library! I am using the old .Net Web API 2 – Rathma Jun 16 '19 at 05:13