gpt4 book ai didi

asp.net-web-api2 - swagger UI 在 webapi 中没有显示任何内容

转载 作者:行者123 更新时间:2023-12-02 09:40:29 26 4
gpt4 key购买 nike

我关注了this直到 xml 文档部分才能使用 Swashbuckle 创建 Swagger 文档。它应该允许我通过(在我的例子中)查看端点:

http://localhost:51854/swagger/ui/index

不幸的是,我看不到任何端点:

enter image description here

有什么想法为什么以及如何解决这个问题吗?请注意,我从一个空的 webapi 项目创建了我的 webapi - 也许这就是问题所在。一定缺少一些东西,但我不确定是什么......

我现在已将以下代码确定为根本原因。在 Global.asax.cs 中:

var container = new XyzWebApiStructureMapContainerConfigurator().Configure(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services
.Replace(typeof(IHttpControllerActivator),
new StructureMapHttpControllerActivator(container));

一些类(class):

public class XyzWebApiStructureMapContainerConfigurator
{
public IContainer Configure(HttpConfiguration config)
{
var container = new Container(new BlaWebApiRegistry());
config.DependencyResolver = new StructureMapDependencyResolver(container);
return container;
}
}

public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver, IHttpControllerActivator
{
private readonly IContainer _container;

public StructureMapDependencyResolver(IContainer container)
: base(container)
{
_container = container;
container.Inject<IHttpControllerActivator>(this);
}

public IDependencyScope BeginScope()
{
return new StructureMapDependencyScope(_container.GetNestedContainer());
}

public IHttpController Create(
HttpRequestMessage request,
HttpControllerDescriptor controllerDescriptor,
Type controllerType)
{
var scope = request.GetDependencyScope();
return scope.GetService(controllerType) as IHttpController;
}
}

附注:

简化的 Controller 代码:

[RoutePrefix("api/XYZ")]
public class BlaController : ApiController
{
private readonly ISomething _something;

public BlaController(ISomething something)
{
_something = something;
}

[Route("")]
[HttpGet]
public IHttpActionResult Resources([FromUri] BlaRequest blaRequest)
{
// something exciting
return Ok(returnObject);
}
}

PPS:

更多代码:

//WebApiConfig

public static void Register(HttpConfiguration config)
{
// Web API configuration and services

// Web API routes
config.MapHttpAttributeRoutes();

//var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors();

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

//Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

var container = new XyzWebApiStructureMapContainerConfigurator().Configure(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services
.Replace(typeof(IHttpControllerActivator),
new StructureMapHttpControllerActivator(container));
}
}

购买力平价:

{
swagger: "2.0",
info: {
version: "v1",
title: "Bla.Di.Bla"
},
host: "localhost:51854",
schemes: [
"http"
],
paths: { },
definitions: { }
}

最佳答案

结果是这一行:

config.DependencyResolver = new StructureMapDependencyResolver(container);

在问题的类 XyzWebApiStructureMapContainerConfigurator 中引起了一些问题。

希望这对将来的人有帮助。

关于asp.net-web-api2 - swagger UI 在 webapi 中没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37032735/

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