custom additional information in API documentation

2019-06-08 03:25发布

enter image description here

I've read here about additional information of web API help page. The data annotation actually provides the additional information for documentation. But I want to know that is there anyway to provide additional information without data annotations?

If yes then how?

If not then is there anyway to override the additional information with data annotations for instance the

[Required]

shows Required written in additional information but what if I want to show "This field is required" or something like that?

Thanks

EDIT see in picture I want to update that additional information without data annotation if possible.

3条回答
仙女界的扛把子
2楼-- · 2019-06-08 03:44

If you want to give custom additional information(using data annotation) then @Pedro G. Dias's answer is your solution but if you want to give additional information without using data annotation then I am afraid that it is not possible OR you have to use some alternative procedure to do so as commented by @DynamicVariable on your question.

PS. I've debugged documentation project to check and I found that addition information is actually provided by data annotations.

查看更多
时光不老,我们不散
3楼-- · 2019-06-08 03:55

You can edit the Required Attribute in the ModelDescriptionGenerator.cs
Areas>HelpPage>ModelDescriptions>ModelDescriptionGenerator.cs
For example:

    [Required(ErrorMessage ="Must pass")]
    public string Name { get; set; }

I got: Additional information : Must pass

replace:

 { typeof(RequiredAttribute), a => "Required" }

with:

{ typeof(RequiredAttribute), a => {
            RequiredAttribute b =(RequiredAttribute)a;
            return (b.ErrorMessage);
        }

see

查看更多
放荡不羁爱自由
4楼-- · 2019-06-08 04:06

So the annotation allows you to further specify requirements, i.e if you have the following model:

public class MyModel {

    [Required(ErrorMessage = "You seriously need a name here bro")]
    public string Name{ get; set; }

}

You can then automatically have the validation message shown in your ASP.Net page like so:

@model string
@Html.TextBoxFor(m => m)
@Html.ValidationMessageFor(model => model, "", new { @class = "text-danger"})

So basically, you add a field for the validation message that will be populated by ASP.Net when the Required attribute kicks in.

查看更多
登录 后发表回答