c# - 无法让 ApiVersion 在 Web Api C# 中工作

标签 c# asp.net asp.net-web-api2

我正在尝试使用我在一个流行的在线站点上找到的教程来控制我的 api 调用。我无法让路由正常工作。我设置了两个 Controller ,版本 1 和版本 2,但我总是被路由到版本 2。就其值(value)而言,我发现教程中的代码似乎有同样的问题。

Controller 1:

using System.Collections.Generic;
using System.Web.Http;
using Microsoft.Web.Http;

namespace WebDemoAPI.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/values")]
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            //throw new NotImplementedException("");
            return new string[] { "value1", "value2" };
        }
        // GET api/values/5
        public string Get(int id){return "value";}
        // POST api/values
        public void Post([FromBody]string value) { }
        // PUT api/values/5
        public void Put(int id, [FromBody]string value) { }
        // DELETE api/values/5
        public void Delete(int id) { }
    }
}

Controller 2:

using System.Collections.Generic;
using System.Web.Http;
using Microsoft.Web.Http;

namespace WebDemoAPI.Controllers
{
    [ApiVersion("2.0")]
    [Route("api/values")]
    public class Values2Controller : ApiController
    {
        // GET: api/Values2
        public IEnumerable<string> Get()
        {
            return new string[] { "version2", "version2" };
        }

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

        // POST: api/Values2
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/Values2/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Values2/5
        public void Delete(int id)
        {
        }
    }
}

网络 API 配置:

using System.Web.Http;
using Microsoft.Web.Http;
using Microsoft.Web.Http.Versioning;

namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.AddApiVersioning(ver =>
            {
                ver.ReportApiVersions = true;
                ver.AssumeDefaultVersionWhenUnspecified = true;
                ver.DefaultApiVersion = new ApiVersion(1, 0);
                ver.ApiVersionReader = new HeaderApiVersionReader("version");
                ver.ApiVersionSelector = new CurrentImplementationApiVersionSelector(ver);
            });

            config.MapHttpAttributeRoutes();

        }
    }
}

我尝试将 header 键设置为版本,将值设置为 1.0,但我被路由到版本 2.0 的 Controller 。将 header 值设置为 2.0 有效。离开 header 键/值也会路由到 Controller 2。

最佳答案

Microsoft.Web.Http.Versioning 命名空间中有 4 个预定义的版本选择器:

CurrentImplementationApiVersionSelector 如果请求中没有指定,则选择最新的 api 版本。
LowestImplementedApiVersionSelector 如果请求中未指定,则选择最低的 api 版本。
ConstantApiVersionSelector 如果在请求中没有指定,则选择在构造函数中传递的常量 api 版本。
DefaultApiVersionSelectorApiVersioningOptions 中选择 DefaultApiVersion 如果在请求中没有指定。

因此,如果您希望默认选择 DefaultApiVersion,则需要使用 DefaultApiVersionSelector

//or just remove this line since this is a default selector
ver.ApiVersionSelector = new DefaultApiVersionSelector(ver);

如果版本控制仍然不起作用,您可能以错误的方式或其他方式设置了 Version header 值。您还可以检查它是否适用于其他版本的阅读器,例如 QueryStringApiVersionReader。更新配置

ver.ApiVersionReader = new QueryStringApiVersionReader("version");

并在查询字符串中传递版本

/api/values?version=1

关于c# - 无法让 ApiVersion 在 Web Api C# 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54698303/

相关文章:

c# - 发送 multipart/form-data 内容类型请求

angularjs - 如何在 Angular 资源中设置owin token 身份验证 header

C# 将列表与自定义对象进行比较但忽略顺序

c# - Entity Framework -通过同一列中的多个条件选择-引用表

c# - 是否可以在 java 中绑定(bind) jTable 的数据源,就像我们在 c# 中的 DataGridView 中所做的那样?

javascript - 如何将 .js 文件添加到 ASP.NET 项目中的程序集

javascript - 预览图像而不上传到我的服务器

javascript - 将值从 JQuery Datepicker 传递到代码隐藏

c# - 使用 BadRequest (WebApi) 返回错误列表

c# - Microsoft Graph API 调用无限期挂起