一、Swagger配置
1、注解不顯示
SwaggerConfig文件下
//c.IncludeXmlComments(GetXmlCommentsPath()); 內下面添加:
c.IncludeXmlComments(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"bin\YDShengya_WebApi.xml"));
然后報錯
未能找到輸出的XML文件 -我去bin目錄也沒有找到
具體解決方案:
原本調試環境下配置的

在發布配置下Release 也配置輸出生成XML才ok

保存即可發布-包含XML的文件
注意:運行時提示
請將注冊表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)設置為 1
則
即可。
2、使用Swagger 讓某些接口不顯示在文檔
在Action 上加[ApiExplorerSettings(IgnoreApi = true)]
[HttpGet]
[ApiExplorerSettings(IgnoreApi = true)]
public ActionResult Index(string appKey , string username )
{
//todo
}
3、Swagger默認路由
Core是在StartUp.cs文件配置

基於framework 配置swagger
指定 route rule 使用Swashbuckle.Application.RedirectHandler,實現原理----route template 完全沒有 request 參數時導向 \swagger。
- 想了解如何轉導,請參考 RedirectHandler.cs
routes.MapHttpRoute(
name: "swagger_root",
routeTemplate: "",
defaults: null,
constraints: null,
handler: new Swashbuckle.Application.RedirectHandler((message => message.RequestUri.ToString()), "swagger")
);

隨機更改
config.Routes.MapHttpRoute(
name: "swagger_root",
routeTemplate: "",
defaults: null,
constraints: null,
handler: new Swashbuckle.Application.RedirectHandler((message => message.RequestUri.ToString()), "swagger/ui/index")
);

這里不做隨意更改直接用Swagger作為root具體代碼參考
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace YDShengya_WebApi
{
/// <summary>
/// 設定預設導向 Swashbuckle 頁面
/// </summary>
public class RouteConfig
{
/// <summary>
/// 在 RouteConfig 中加入一組 route rule 指定 route rule 使用 Swashbuckle.Application.RedirectHandler
/// </summary>
/// <param name="routes"></param>
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "swagger_root",
routeTemplate: "",
defaults: null,
constraints: null,
handler: new Swashbuckle.Application.RedirectHandler((message => message.RequestUri.ToString()), "swagger")
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
3、MVC和WebApi路由區別匯總 WebApi的默認路由機制通過http請求的類型匹配Action,MVC的默認路由機制通過url匹配Action WebApi的路由配置文件是WebApiConfig.cs,MVC的路由配置文件是RouteConfig.cs WebApi的Controller繼承自Web.Http.ApiController,MVC的Controller繼承自Web.Mvc.Controller
