示例#1
0
        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/XmlDocumentation.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"}}
            //});

            //// 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("NOT IMPLEMENTED", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));
            config.SetSampleForType("NOT IMPLEMENTED", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(String));

            //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
            //// and action named "Put".
            //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

            //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
            //// on the controller named "Values" and action named "Get" with parameter "id".
            //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

            //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
            //config.SetActualRequestType(typeof(string), "Values", "Get");

            //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
            //config.SetActualResponseType(typeof(string), "Values", "Post");
        }
示例#2
0
        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("~/bin/myDoc.xml")));
            //config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/myDoc.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(CityResult), "sample string"},
            //    {typeof(IEnumerable<CityResult>), new string[]{"sample 1", "sample 2"}}
            //});

            // Extend the following to provide factories for types not handled automatically (those lacking parameterless
            // constructors) or for which you prefer to use non-default property values. Line below provides a fallback
            // since automatic handling will fail and GeneratePageResult handles only a single type.
            #if Handle_PageResultOfT
            config.GetHelpPageSampleGenerator().SampleObjectFactories.Add(GeneratePageResult);
            #endif

            // Extend the following to use a preset object directly as the sample for all actions that support a media
            // type, regardless of the body parameter or return type. The lines below avoid display of binary content.
            // The BsonMediaTypeFormatter (if available) is not used to serialize the TextSample object.
            //config.SetSampleForMediaType(
            //    new TextSample("Binary JSON content. See http://bsonspec.org for details."),
            //    new MediaTypeHeaderValue("application/bson"));

            //// 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&[2]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));  //william uncomment

            //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
            //// and action named "Put".
            //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

            //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
            //// on the controller named "Values" and action named "Get" with parameter "id".
            //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

            //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
            // config.SetActualRequestType(typeof(string), "City", "Get");

            //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
            //config.SetActualResponseType(typeof(string), "Values", "Post");
            //config.SetActualResponseType(typeof(City), "City", "Get");
            //config.SetActualResponseType(typeof(CityList), "City", "Get");
            //config.SetActualResponseType(typeof(RamResource), "Resource", "Get");
            //config.SetActualResponseType(typeof(Search_Result), "Search", "Get");
            //config.SetActualResponseType(typeof(ProvinceList), "Province", "Get");
            //config.SetActualResponseType(typeof(SubCategoryResult), "SubCategory", "Get");
            //config.SetActualResponseType(typeof(TopCategoryList), "TopCategory", "Get");
            //config.SetActualResponseType(typeof(TopCategory_Result), "TopCategory", "Get");
            //config.SetActualResponseType(typeof(SuggestionWords), "Search", "Get");
        }
        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/ContactManager.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(Contact), SampleData.Contacts[0]},
                {typeof(IEnumerable<Contact>), SampleData.Contacts}
            });

            //// 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(
                "Name=Daniel Roth&Address=One+Microsoft+Way&City=Redmond&State=WA&Zip=98011&Email=&Twitter=",
                new MediaTypeHeaderValue("application/x-www-form-urlencoded"),
                typeof(Contact));
            config.SetSampleForType(
                new ImageSample(HttpContext.Current.Server.MapPath("~/Images/Image1.png")),
                new MediaTypeHeaderValue("image/png"),
                typeof(Contact));

            //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
            //// and action named "Put".
            //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

            //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
            //// on the controller named "Values" and action named "Get" with parameter "id".
            //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

            //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
            //config.SetActualRequestType(typeof(string), "Values", "Get");

            //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
            config.SetActualResponseType(typeof(Contact), "Contacts", "Post");
        }
示例#4
0
        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/XmlDocumentation.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), "exempel"},
                {typeof(IEnumerable<string>), new string[]{"exempel 1", "exempel 2"}}
            });

            // Extend the following to provide factories for types not handled automatically (those lacking parameterless
            // constructors) or for which you prefer to use non-default property values. Line below provides a fallback
            // since automatic handling will fail and GeneratePageResult handles only a single type.
            #if Handle_PageResultOfT
            config.GetHelpPageSampleGenerator().SampleObjectFactories.Add(GeneratePageResult);
            #endif

            // Extend the following to use a preset object directly as the sample for all actions that support a media
            // type, regardless of the body parameter or return type. The lines below avoid display of binary content.
            // The BsonMediaTypeFormatter (if available) is not used to serialize the TextSample object.
            config.SetSampleForMediaType(
                new TextSample("Binary JSON content. See http://bsonspec.org for details."),
                new MediaTypeHeaderValue("application/bson"));

            // 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("FirstName=sample1&LastName=sample2&BossId=sampleBoss&[email protected] &AdTitle=sample3&OwnTitle=sample4&Seat=sample5&UserName=sample6&Phone=1234&Mobile=1234&Office=sample7 &Company=sample8&PersonalInfo=sample9&Active=true&CostCentre=15", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(SimpleUser));

            //// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
            //// and action named "Put".
            //config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

            //// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
            //// on the controller named "Values" and action named "Get" with parameter "id".
            //config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

            //// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
            //config.SetActualRequestType(typeof(string), "Values", "Get");

            //// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
            //// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
            //config.SetActualResponseType(typeof(string), "Values", "Post");
        }
        private static void SetSample(HttpConfiguration config, object sample)
        {
            var json = JsonConvert.SerializeObject(sample, Formatting.Indented);
            var xml = CreateXml(sample);
            var sampleType = sample.GetType();
            var gen = config.GetHelpPageSampleGenerator();

            var mimeTypes = new List<MediaTypeHeaderValue>
            {
                new MediaTypeHeaderValue("application/json"),
                new MediaTypeHeaderValue("text/json"),
                new MediaTypeHeaderValue("text/html"),
                new MediaTypeHeaderValue("text/plain")
            };

            foreach (var mediaType in mimeTypes)
            {
                var smpl = gen.ActionSamples.FirstOrDefault(x =>
                    Equals(x.Key, new HelpPageSampleKey(mediaType, sampleType)));
                if (smpl.Value != null)
                    gen.ActionSamples[smpl.Key] = json;
                else config.SetSampleForType(json, mediaType, sampleType);
            }

            mimeTypes = new List<MediaTypeHeaderValue>
            {
                new MediaTypeHeaderValue("text/xml"),
                new MediaTypeHeaderValue("application/xml")
            };
            foreach (var mediaType in mimeTypes)
            {
                var smpl = gen.ActionSamples.FirstOrDefault(x =>
                    Equals(x.Key, new HelpPageSampleKey(mediaType, sampleType)));
                if (smpl.Value != null)
                    gen.ActionSamples[smpl.Key] = xml;
                else config.SetSampleForType(xml, mediaType, sampleType);
            }
        }
 public void SetSampleForType()
 {
     HttpConfiguration config = new HttpConfiguration();
     ImageSample sample = new ImageSample("http://localhost/test.png");
     config.SetSampleForType(sample, new MediaTypeHeaderValue("image/png"), typeof(string));
     object sampleGeneratorObj;
     config.Properties.TryGetValue(typeof(HelpPageSampleGenerator), out sampleGeneratorObj);
     HelpPageSampleGenerator sampleGenerator = Assert.IsType<HelpPageSampleGenerator>(sampleGeneratorObj);
     Assert.NotEmpty(sampleGenerator.ActionSamples);
     var actionSample = sampleGenerator.ActionSamples.First();
     Assert.Equal(String.Empty, actionSample.Key.ControllerName);
     Assert.Equal(String.Empty, actionSample.Key.ActionName);
     Assert.Equal(new MediaTypeHeaderValue("image/png"), actionSample.Key.MediaType);
     Assert.Null(actionSample.Key.SampleDirection);
     Assert.Empty(actionSample.Key.ParameterNames);
     Assert.Same(sample, actionSample.Value);
 }