MVC WEB api 自動生成文檔

最近在一直在用webapi作接口給移動端用。可是讓我糾結的時候每次新加接口或者改動接口的時候,就須要從新修改文檔這讓我非常苦惱。無心中發現。webapi竟然有自動生成文檔的功能。。。。真是看見了救星啊。web

在看了一些資料後發現,若是你的開發環境比較老的話像VS2010 VS2008 這樣的你可能須要手動在nuGet去安裝一個新的組件,api

須要安裝這一個組件來進行配置,安裝完成後會多一個文件夾(由於這個版本較新可能會有依賴版本衝突) app

若是你是2013的版本的話你在建立項目的時候默認就會有這個文件夾,固然我指的是建立webapi項目ide

 這裏面我要着重講的就是下面這個類,這個類是用來配置幫助頁的。this

下面我來分步來講明我們配置url

首先你須要把第一行代碼取消註釋spa

using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;

namespace Cidtech.FaceProject.Areas.HelpPage
{
    /// <summary>
    /// Use this class to customize the Help Page.
    /// For example you can set a custom <see cref="System.Web.Http.Description.IDocumentationProvider"/> to supply the documentation
    /// or you can provide the samples for the requests/responses.
    /// </summary>
    public static class HelpPageConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //// 這裏是設置生成文檔的路徑的 生成文檔的第一步,首先你都把這行代碼取消註釋而後記下默認的路徑
            config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

            //// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
            //// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type 
            //// formats by the available formatters.這個是用來格式話參數的默認是不啓用的
            //config.SetSampleObjects(new Dictionary<Type, object>
            //{
            //    {typeof(string), "sample string"},
            //    {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
            //});

            //// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
            //// and have IEnumerable<string> as the body parameter or return type.
            //config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));

            //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
            //// and action named "Put".
            //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

            //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
            //// on the controller named "Values" and action named "Get" with parameter "id".
            //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

            //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
            //config.SetActualRequestType(typeof(string), "Values", "Get");

            //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
           // config.SetActualResponseType(typeof(string), "Values", "Post");
        }
    }
}

  第二步就是你須要在你的項目裏設置下生XML的路徑code

 

 兩個路徑要相同。orm

其實道理這一部就算是基本完成了。而後你若是要看效果的話運行項目.xml

這裏面的描述信息其實就這你在api裏面寫的註釋像這樣的

到這裏位置。整個步驟就算完成了。若是你須要簡單配置的話就這樣,就能夠用了。抽時間我再詳細把整個程序運行流程在解釋一遍,這裏就不詳加解釋了有不懂的能夠私信我。

相關文章
相關標籤/搜索