在AspNet WebApi帮助页面中生成模型描述 [英] Generate model description in AspNet WebApi help pages

查看:55
本文介绍了在AspNet WebApi帮助页面中生成模型描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Asp.Net Web Api帮助页面中为模型生成说明?

How do I generate a description for my model in Asp.Net Web Api help pages?

示例:

从示例中可以看到,我已经可以生成 Name Type Additional Information .但是如何生成说明?

As you can see from the example, I can already generate Name, Type and Additional Information. But how do I generate Description?

我什么也没尝试,我全都没主意.

不,那不是事实.我尝试将注释添加到我的 TransactionDto 类中,但是它不起作用.

No, that's not true. I've tried adding comments to my TransactionDto class, but it does not work.

/// <summary>
/// A DTO (Data Transfer Object) for Transaction objects.
/// </summary>
public class TransactionDto
{
    /// <summary>
    /// The manager who registered the transaction.
    /// </summary>
    public string FromId { get; set; }

    /// <summary>
    /// The receiving manager.
    /// </summary>
    [Required]
    public string ToId { get; set; }

    /// <summary>
    /// Optional expiration date.
    /// </summary>
    public DateTime? Expires { get; set; }

    /// <summary>
    /// Date the transaction was created.
    /// </summary>
    public DateTime Created { get; set; }
}

我已将 HelpPageConfig.cs 配置为使用 XmlDocumentationProvider ,如下所示:

I have configured HelpPageConfig.cs to use an XmlDocumentationProvider like so:

config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

那我该如何为模型生成这些描述呢?

So how do I generate these descriptions for models?

推荐答案

我相信您在Web API项目之外的其他项目中有模型吗?

I believe you have models in the separate project other than the Web API project?

如果是这种情况,则Web API项目不知道为模型生成的帮助XML文件.您需要同时为Web API项目和模型项目设置XML输出路径,然后在HelpPageConfig.cs文件的register方法中组合这两个XML文件.

If that is the case, the web API project is not aware of the help XML file generated for the models. you need to set the XML output path for both web API project and the models project and then combine both XML files in the register method of HelpPageConfig.cs file.

public static void Register(HttpConfiguration config)
{
        XmlDocument apiDoc = new XmlDocument();
        apiDoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/WebApi.Orders.xml"));
        XmlDocument contractsDoc = new XmlDocument();
        contractsDoc.Load(HttpContext.Current.Server.MapPath("~/App_Data/Contracts.xml"));
        if (contractsDoc.DocumentElement != null && apiDoc.DocumentElement!=null)
        {
            XmlNodeList nodes = contractsDoc.DocumentElement.ChildNodes;
            foreach (XmlNode node in nodes)
            {
                XmlNode copiedNode = apiDoc.ImportNode(node, true);
                apiDoc.DocumentElement.AppendChild(copiedNode);
            }
            apiDoc.Save(HttpContext.Current.Server.MapPath("~/App_Data/WebApi.Orders.xml"));
        }
        config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/WebApi.Orders.xml")));
    ......
}

这篇关于在AspNet WebApi帮助页面中生成模型描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆