我跟随Azure Application Gateway到xml doc部分,以便使用Swashbuckle创建Swagger文档。它应该允许我通过(在我的情况下)查看端点:
不幸的是,我看不到任何终点:
http://localhost:51854/swagger/ui/index
任何想法为什么以及如何解决这个问题?请注意,我从一个空的webapi项目创建了我的webapi - 也许这就是问题所在。必须丢失一些东西,但我不确定是什么......
我现在已将以下代码确定为根本原因。在Global.asax.cs中:
var container = new XyzWebApiStructureMapContainerConfigurator().Configure(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services
.Replace(typeof(IHttpControllerActivator),
new StructureMapHttpControllerActivator(container));
某些课程:
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;
}
}
PS:
简化的控制器代码:
[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));
}
}
PPPS:
{
swagger: "2.0",
info: {
version: "v1",
title: "Bla.Di.Bla"
},
host: "localhost:51854",
schemes: [
"http"
],
paths: { },
definitions: { }
}
答案 0 :(得分:2)
原来这一行:
config.DependencyResolver = new StructureMapDependencyResolver(container);
在问题的类XyzWebApiStructureMapContainerConfigurator中引起了一些问题。
希望这有助于将来。
答案 1 :(得分:0)
有同样的问题。原来,DI(在我的情况下为Unity)已配置为绑定所有已加载的程序集(其中一个是Swashbuckle.Core)。
只需细化绑定的程序集即可解决此问题:
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(asm => asm.FullName.StartsWith("MySolution.MyProject"));
// Web API configuration and services
var container = new UnityContainer();
container.RegisterTypes(
AllClasses.FromAssemblies(assemblies),
WithMappings.FromMatchingInterface,
WithName.Default);
config.DependencyResolver = new UnityDependencyResolver(container);
答案 2 :(得分:0)
我同样也招摇摇晃,花了很长时间寻找解决方案。终于得到了。 在每个控制器的操作方法中添加[HttpGet]或[HttpPost]属性。
[HttpPost]
public IActionResult TestMethod()
{
//// Some code
}