API版本是必需的,但未指定。 webapi [英] An API version is required, but was not specified. webapi

查看:266
本文介绍了API版本是必需的,但未指定。 webapi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var constraintResolver = new DefaultInlineConstraintResolver()
{
    ConstraintMap =
    {
        ["apiVersion"] = typeof( ApiVersionRouteConstraint )
    }
};

config.MapHttpAttributeRoutes(constraintResolver);
config.AddApiVersioning(o => o.AssumeDefaultVersionWhenUnspecified = true);


[ApiVersion("2.05")]
[RoutePrefix("api/v{version:apiVersion}/ger")]
public class caGerController
[Route("~/api/ger/getDetail")]
[Route("getDetail")]
 GetGerData


[ApiVersion("1")]
[RoutePrefix("api/v{version:apiVersion}/gerDetail")]

public class caGerDetailsController
caGerController
[Route("~/api/gerDetail/getDetail")]
[Route("getDetail")]
 GetGerData

>>  GetGerData

结果:
1)两个URL均与v1版本ROUTE一起使用。
2)第二个网址同时适用于v1和无v1路由的直接网址,即[Route(〜/ api / gerDetail / getDetail)]

result: 1) both URL working with v1 version ROUTE. 2) second URL working for both, v1 and direct without v1 route as well i.e. [Route("~/api/gerDetail/getDetail")]

3)问题:第一个URL仅适用于v1,而不适用于诸如[[Route(〜/ api / ger / getDetail)]
之类的直接路由,并出现如下错误:

3) PROBLEM: first URL is only working with v1 and its not working with direct route like " [Route("~/api/ger/getDetail")]" and getting an error as below:

"Error": {
        "Code": "ApiVersionUnspecified",
        "Message": "An API version is required, but was not specified."
}

如何解决此问题。
当我从2.05更改为1.0时,它可以工作,但2.0或2.05都不能工作。有单独的文件夹需要吗?

How to solve this issue. When I change from 2.05 to 1.0 then it works but 2.0 or 2.05 both not work. Is there something separate folder require ?

推荐答案

ApiVersionUnspecified 发生的原因是全部 >默认情况下,路由需要一个显式的API版本。您可以使用以下选项退出此行为:

The ApiVersionUnspecified happens because all routes require an explicit API version by default. You opt out of this behavior using:

options.AssumeDefaultVersionWhenUnspecified = true

此设置表示客户端不提供默认API版本。默认值为:

This setting means that a default API version is assumed when a client doesn't provide one. The default value is:

options.DefaultApiVersion // equals 1.0 by default

使用URL段版本控制方法时,不能有两个同时为未版本化路由的不同控制器。没有API版本的路由只能映射到单个控制器。由于默认值为 1.0,并且您有一个具有 unversioned 路由的控制器,因此该控制器将始终被匹配。

When you use the URL segment versioning method, you can't have two different controllers that both an unversioned route. The route without an API version can only map to a single controller. Since the default is "1.0" and you have a controller with the unversioned route, that's the one that will always been matched.

这篇关于API版本是必需的,但未指定。 webapi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆