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.

1
  • Your question helped me understand how to implement versioning. I was searching for examples. Commented Aug 20, 2019 at 11:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow ! It really worked, That was a very subtle point that I missed.
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 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.
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.

1 Comment

I guess you are working with .net core version of this library! I am using the old .Net Web API 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.