2016-02-03 92 views
0

我想將Swashbuckle添加到我的ASP.NET MVC WebAPI項目中。我已經開始在Visual Studio 2013中使用一個空的MVC項目模板。後來我添加了所需的文件/包等。但不知何故,我無法設法將Swashbuckle集成到其中。每次運行此代碼時,在SwashConfig.cs中給出以下錯誤。Swashbuckle配置錯誤

Swashbuckle Config

以下是我web.config

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.6" /> 
    <httpRuntime targetFramework="4.6" /> 
    </system.web> 
<system.webServer> 
    <handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 
    </system.webServer></configuration> 

package.config

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" /> 
    <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" /> 
    <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" /> 
    <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" /> 
    <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" /> 
    <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net46" /> 
    <package id="Swashbuckle" version="5.2.2" targetFramework="net46" /> 
    <package id="Swashbuckle.Core" version="5.2.2" targetFramework="net46" /> 
    <package id="WebActivatorEx" version="2.1.0" targetFramework="net46" /> 
</packages> 

Global.asax.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Http; 
using System.Web.Mvc; 
using System.Web.Routing; 
using System.Web.Security; 
using System.Web.SessionState; 

namespace CDE.API 
{ 
    public class Global : System.Web.HttpApplication 
    { 

     protected void Application_Start(object sender, EventArgs e) 
     { 
      GlobalConfiguration.Configure(WebApiConfig.Register); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 

      GlobalConfiguration.Configure(config => SwaggerConfig.Register(config)); 
     } 
    } 
} 

WebApiConfig.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net.Http; 
using System.Web.Http; 
using Newtonsoft.Json.Serialization; 

namespace CDE.API 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Web API routes 
      config.MapHttpAttributeRoutes(); 

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

      var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); 
      config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); 
     } 
    } 
} 

SwaggerConfig.cs

using System.Web.Http; 
using WebActivatorEx; 
using CDE.API; 
using Swashbuckle.Application; 

//[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] 

namespace CDE.API 
{ 
    public class SwaggerConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      var thisAssembly = typeof(SwaggerConfig).Assembly; 

      config 
       .EnableSwagger(c => 
       { 
        c.SingleApiVersion("v1", "CDE.API"); 
        c.IncludeXmlComments(string.Format(@"{0}\bin\CDE.API.XML", System.AppDomain.CurrentDomain.BaseDirectory)); 
        c.DescribeAllEnumsAsStrings(); 
       }) 
       .EnableSwaggerUi(c => 
       { 
       }); 
     } 
    } 
} 

RouteConfig.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 

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

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}" 
      ); 
     } 
    } 
} 

東西是非常錯誤的。請幫忙。

我使用:

System.Web.Http Version

System.Web.Http.WebHost Version

回答

1

我想在您去錯了可能是註釋掉這行代碼上SwaggerConfig: -

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] 

然後,您決定在RoutesConfig註冊完所有路由後決定運行這一行代碼,

GlobalConfiguration.Configure(config => SwaggerConfig.Register(config));

您應該嘗試刪除上面的行,並嘗試按照默認行爲允許SwaggerConfig在PreApplicationStartup上運行。

對我來說這個錯誤信息(我只用了Web API的招搖)建議你試圖在管道後期將swagger的路由添加到0。

祝你好運。