2017年7月25日 星期二

[MVC] Create API and Help page on MVC 5

  • 選擇 Web API
  • 關閉 AD 驗證
  • 基礎的範本程式碼
  • 步驟1: 手動加入 XML 文件檔案
    • 此步驟會幫助,程式中所撰寫的 <summary> 資訊代入 Help Page
    • 查尋 HelpPageComfig.cs 中修改資訊

    • Before Code: 開始是被註解起來
public static void Register(HttpConfiguration config)
{
    //// Uncomment the following to use the documentation from XML documentation file.
    //config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
    ...
}
C#
    • After Code: 解註解並修改對應的 XML 路徑
public static void Register(HttpConfiguration config)
{
    //// Uncomment the following to use the documentation from XML documentation file.
    config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
    ...
}
C#
  • 實作 Get / Post
    • Class Code
    namespace WebAPIDemo.Models
    {
        ///
        /// User Data
        ///
        public class User
        {
            ///
            /// User Name
            ///
            public string Name { get; set; }
            ///
            /// User Age
            ///
            public int Age { get; set; }
        }
    }
    C#
    • Get / Post API 
    using System.Web.Http;
    using WebAPIDemo.Models;
    
    namespace WebAPIDemo.Controllers
    {
        ///
        /// How to using Web API
        ///
        public class DemoController : ApiController
        {
            ///
            /// Get User Info
            ///
            ///  User Info
            public User Get()
            {
                User user = new User()
                {
                    Name = "Lyndon",
                    Age = 30
                };
    
                return user;
            }
    
            ///
            /// Add User Age
            ///
            ///  User Info
            public User Post(User Info)
            {
                Info.Age = Info.Age + 10;
    
                return Info;
            }
        }
    }
    C#
    • Postmen Get
    • Postmen Post: 需要傳入參數
    • Help Info 
      • <summary> 所寫入的資訊都會出現在 Help 中

  • POST 有些參數必輸入資訊,需進行下列修改
    • Code
    using System.ComponentModel.DataAnnotations;
    
    namespace WebAPIDemo.Models
    {
        ///
        /// User Data
        ///
        public class User
        {
            ///
            /// User Name
            ///
            [Required]
            [StringLength(10, ErrorMessage = "請勿輸入超過10個字")]
            public string Name { get; set; }
            ///
            /// User Age
            ///
            public int Age { get; set; }
        }
    }
    C#
    • Help: 加入條件說明
    • POST API Code
      ///
      /// Add User Age
      ///
      ///  User Info
      public User Post(User Info)
      {
          if (ModelState.IsValid)
          {
              // Do something with the user (not shown).
              //return new HttpResponseMessage(HttpStatusCode.OK);
              Info.Age = Info.Age + 10;
              return Info;
          }
          else
          {
              //return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
              return null;
          }
      }
      C#
    • Postmen
  • Ref
    • http://kevintsengtw.blogspot.tw/2015/12/aspnet-web-api-help-page.html
    • https://stackoverflow.com/questions/26098077/microsoft-web-api-help-page-how-to-create-annotations-for-parameters
    • https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api
    • http://kevintsengtw.blogspot.tw/2016/02/aspnet-web-api-import-postman.html



沒有留言:

張貼留言