custom additional information in API documentation

2019-06-08 03:42发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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