首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swashbuckle配置错误

Swashbuckle配置错误
EN

Stack Overflow用户
提问于 2016-02-03 16:44:00
回答 1查看 1.5K关注 0票数 0

我正在尝试将Swashbuckle添加到我的ASP.NET MVC WebAPI项目中。我在Visual Studio 2013中使用了一个空的MVC项目模板。后来我添加了所需的文件/包等,但不知何故我无法将Swashbuckle集成到其中。每次我运行这段代码时,在SwashConfig.cs中都会出现以下错误。

以下是我的web.config

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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}"
            );
        }
    }
}

有些地方非常不对劲。请帮帮忙。

我使用的是:

EN

回答 1

Stack Overflow用户

发布于 2016-02-04 16:49:53

我认为你的错误之处可能是你在SwaggerConfig上注释掉了这行代码:

代码语言:javascript
复制
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

然后,您决定在RoutesConfig注册了所有路由之后运行此行代码,

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

您应该尝试删除上面的行,并尝试根据默认行为允许SwaggerConfig在PreApplicationStartup上运行。

对我来说,这个错误信息..(我只在Web中使用了swagger )建议您尝试在管道的后期添加swagger to0的路由。

祝好运。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35172163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档