- 步驟1: 手動加入 XML 文件檔案
- 此步驟會幫助,程式中所撰寫的 <summary> 資訊代入 Help Page
- 查尋 HelpPageComfig.cs 中修改資訊
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")));
...
}
- 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")));
...
}
namespace WebAPIDemo.Models
{
///
/// User Data
///
public class User
{
///
/// User Name
///
public string Name { get; set; }
///
/// User Age
///
public int Age { get; set; }
}
}
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;
}
}
}
- Help Info
- <summary> 所寫入的資訊都會出現在 Help 中
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; }
}
}
///
/// 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;
}
}
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
沒有留言:
張貼留言