gpt4 book ai didi

c# - 使用 URL c# 的 Web-Api 版本控制

转载 作者:行者123 更新时间:2023-12-04 01:00:43 27 4
gpt4 key购买 nike

我现在已经苦苦挣扎了好几天,我正在尝试将我的 API 实现为带有 url 后缀的版本。f.e. http://localhost/api/v1/sites & http://localhost/api/v2/sites .

我想我已经很接近了,但突然间我把一切都搞砸了..

非常感谢你帮助我。

现在我收到:“System.invalidOperationException:”映射到约束键“apiVersion”的约束类型“ApiVersionRouteConstraint”必须实现 IhttpRouteConstraint 接口(interface)。在 Global.asax.cs 上

Global.asax.cs

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace HIDDEN
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}

WebApiConfig.cs

using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Owin.Security.OAuth;
using System.Web.Http;
using System.Web.Http.Routing;

namespace HIDDEN
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

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

// Web API routes
config.MapHttpAttributeRoutes(contraintResolver);
config.AddApiVersioning(o =>
{
o.AssumeDefaultVersionWhenUnspecified = true;
//o.DefaultApiVersion = new ApiVersion(1, 0);
});

config.Routes.MapHttpRoute(
name: "DefaultApi",
//routeTemplate: "api/v{version:apiVersion}/{controller}/{id}",
routeTemplate: "api/v{version:apiVersion}/{controller}",
//routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

// WebAPI when dealing with JSON & JavaScript!
// Setup json serialization to serialize classes to camel (std. Json format)
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

// Adding JSON type web api formatting.
config.Formatters.Clear();
config.Formatters.Add(formatter);

}
}
}

RouteConfig.cs

using System.Web.Mvc;
using System.Web.Routing;


namespace HIDDEN
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}

站点 Controller

using FirebirdSql.Data.FirebirdClient;
using System;
using System.Web.Http;
using System.Configuration;
using System.Collections;
using Microsoft.Web.Http;

namespace HIDDEN.Controllers.V1
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]

public class SitesController : ApiController
{
//code for V1
}

namespace HIDDEN.Controllers.V2
{
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]

public class SitesController : ApiController
{
//code for V2
}
}

最佳答案

确保您使用的是正确的 ApiVersionRouteConstraint,因为 Microsoft.AspNetCore.Mvc.RoutingMicrosoft.Web.Http 之间似乎存在冲突。路由

如果您正在使用此版本控制库 Microsoft.AspNet.WebApi.Versioning你应该使用 Microsoft.Web.Http.Routing

using Microsoft.Web.Http.Routing;

此外,您还可以向路由添加 apiVersion 约束,如下所示

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v{apiVersion}/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { apiVersion = new ApiVersionRouteConstraint() }
);

关于c# - 使用 URL c# 的 Web-Api 版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58449150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com