简体   繁体   中英

Web API 2 versioning with Microsoft.AspNet.WebApi.Versioning

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.

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.

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

    [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM