Webapi帮助页面 - 如何创建自定义数据?防御并指定需要什么?

时间:2013-05-03 22:03:32

标签: .net asp.net-web-api

我正在查看Webapi帮助页面以生成文档,但我看到的所有教程都让我感到疑惑。

Q1。 如何自己填充样本数据?根据我的理解,它会查看数据类型并根据数据类型生成一些数据。我的一些数据有特定的要求(即长度不能超过5个字符)。

如何为每种方法编写自己的样本数据?

Q2 如何隐藏警告信息。

我收到此消息

  

无法为媒体类型生成样本   '应用程序/ X WWW的窗体-urlencoded&#39 ;.不能使用formatter   ' JQueryMvcFormUrlEncodedFormatter'写出类型' ProductDM'。

我不确定" x-www-form-urlencoded"但是,如果我不支持,我怎么能隐藏这条消息或者说'#34;不支持"?

Q3如何为每个参数编写说明。在大多数情况下,很清楚它们是什么,但在某些情况下可能不是。如果它自动接受注释并将它们放在它们旁边也可以很好地显示参数A可能是选项而参数B不是。

2 个答案:

答案 0 :(得分:10)

Q1:你看过“Areas \ HelpPage \ App_Start \ HelpPageConfig.cs”文件吗?您应该看到一堆注释的示例,您可以如何定义自己的样本。

示例:

public static class HelpPageConfig
{
    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")));

        //// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
        //// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type 
        //// formats by the available formatters.
        //config.SetSampleObjects(new Dictionary<Type, object>
        //{
        //    {typeof(string), "sample string"},
        //    {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
        //});

Q2:您看到“application / x-www-form-urlencoded”mediatype的错误,因为我们使用的格式化程序只能反序列化或读取数据而无法写入。这里的错误是指示它只能写入样本,但是如果您实际上是在这种媒体类型中发送数据,则可以正确地反序列化。您可以为此媒体类型提供显式示例,而不是隐藏此部分。 HelpPageConfig.cs有以下示例:

//// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
//// and have IEnumerable<string> as the body parameter or return type.
//config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));

问题3:有关操作参数的文档,您始终可以使用常规注释(摘要,参数等)并生成文档文件,并将其指向如下:

//// Uncomment the following to use the documentation from XML documentation file.
//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

我们目前不支持从数据注释生成文档,我们目前有一个问题跟踪它:http://aspnetwebstack.codeplex.com/workitem/877

答案 1 :(得分:0)

关于第2季度“如何隐藏警告消息”,在Areas / HelpPage / Views / Help / DisplayTemplates / Samples.chtml中,您可以在代码中添加if语句:

function copyKey(id) {
            var copyText = document.getElementById(id);
            copyText.select();
            document.execCommand("copy");
        }

        function copy_IP() {
            var copy_Text = document.getElementById('our_ip');

            //create temporary input to copy text as our input is hidden so we need to create another input 
            var tempInput = document.createElement("input");
            tempInput.style = "position: absolute; left: -1000px; top: -1000px";
            tempInput.value = copy_Text.value;
            document.body.appendChild(tempInput);
            tempInput.select();
            document.execCommand("copy");
            document.body.removeChild(tempInput);

            //show success message
            toastr.info(copyText.value, 'IP Copied to Clipboard');
        }

就我而言,我已经在显示POST数据结构,因此不需要“ x-www-form-urlencoded”部分。默认情况下,helpPageConfig还显示查询字符串,而我的方法仅接受对象。