Skip to main content
 首页 » 编程设计

c#之逐步在 ASP.Net Web API 上运行 swashbuckle

2024年02月01日26birdshome

我通过下载 Swashbuckle 开始运行项目“Swashbuckle.Dummy.SelfHost”。然后我转到浏览器并检查了在 program.cs 类的主要函数中声明的 URL。当我在浏览器中提供相同的 URL,如“http:/localhost:8090/swagger”时,出现以下错误。谁能指导我一步一步地运行斜扣解决方案并进行测试。如果我想在 IIS 上托管此应用程序并运行我需要遵循的步骤是什么?

An error has occurred. 
 
Could not load file or assembly 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) 
 
System.IO.FileLoadException 
 
at Swashbuckle.Application.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.HttpServer.<>n__FabricatedMethod9(HttpRequestMessage , CancellationToken ) at System.Web.Http.HttpServer.d__0.MoveNext() 

请您参考如下方法:

问题是 Swashbuckle 在版本 4 中使用 System.Web.Http 和 System.Net.Http.Formatting 程序集。我假设您的应用程序使用的是 ASP MVC 5.x。在这种情况下,您必须将绑定(bind)重定向添加到您的 web.config 中。 将以下部分添加到您的 web.config 中(将“5.0.0.0”替换为您的应用程序实际引用的上述程序集的版本):

  <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
        <dependentAssembly> 
            <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral"/> 
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/> 
        </dependentAssembly> 
        <dependentAssembly> 
            <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral"/> 
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/> 
        </dependentAssembly> 
    </assemblyBinding> 
  </runtime>