私のアプリケーションはASP.NETCore 1.0 WebAPIです。
私は次のコントローラーを持っています:
[HttpGet("{someData:MinLength(5):MaxLength(5)}")]
[Produces("application/json")]
public async Task<IActionResult> GetSomeData(string someData)
{
return this.Ok(JsonConvert.SerializeObject("Data is: " + someData));
}
たとえば、文字列「111」を渡すと、swaggerは次のメッセージを表示します。
次のようなレスポンスボディを実現するにはどうすればよいですか。
「5つの数字を入力してください」
ありがとうございました
[ProducesResponseType(typeof(ModelStateDictionary), (int)HttpStatusCode.OK)]
を使用する場合のように、アクションに注釈を付けて、一般的な辞書タイプのエラーメッセージを返すことができますreturn BadRequest(ModelState)
。
ただし、スキーマ定義でテキストを返すことはできません。これは、エラーメッセージではなく、json構造用にあります。代わりに、xmldocを使用して(そしてswaggerで有効にする)、パラメーターに説明を追加する必要があります。
/// <summary>
/// Returns some data based on <paramref name="someData"/> parameter.
/// </summary>
/// <param name="someData">Some data. (Must be exactly 5 characters wide)</param>
/// <response code="200">Returns indexed tags on success</response>
/// <response code="400">Invalid data sent</response>
/// <returns>A paged list of results</returns>
[HttpGet("{someData:MinLength(5):MaxLength(5)}")]
[ProducesResponseType(typeof(MyReturnType), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(void), (int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetSomeData(string someData)
{
}
また、プロジェクトのプロパティでxmldocsのビルドを有効にする必要があります。
そして、スタートアップに追加します。
services.AddSwaggerGen(options =>
{
...
var appEnv = PlatformServices.Default.Application;
options.IncludeXmlComments(Path.Combine(appEnv.ApplicationBasePath, $"{appEnv.ApplicationName}.xml"));
...
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加