2

ASP.NET Web API has an easy install Nuget help page with sample generator. It's easy to get it to generate and display sample requests, but not so easy it seems to get it to display sample responses (httpsampleresponses) so that when developers look at the help page they'd see examples of generated responses / not static/typed in responses, but actually generated. I've seen it done before on another project, but still having trouble figuring out how to do it. MSDN's YAO has a good blog but it's just not getting me all the way to success for some reason.

From what I've seen work live and based on what there is to read about it online, it's definitely in getting the HelpPageConfig file right in terms of the config.SetSampleResponses() set up. I've discovered the configuration file that sets the parameters for the SetSampleResponses() method, but still, nothing I try is working. It was suggested to me that I should create a custom type and use extension methods, but getting that to correspond and display what I need hasn't happened yet. I can get it to compile without errors, but it still doesn't show the generated response sample on the page. It was easy with the SetSampleForType piece to get a section to show up in the requests section, but it's the response part that has given me trouble.

Has anyone out there done this with the SetSampleResponses() successfully and is there any kind of trick you can clearly define for getting it to work? Do you have any tips on setting up a specific generic type and making that work?

I'm thinking this must be something really simple and I'm just not clicking to make it happen....

Thanks for any potential info...

user1258050
  • 21
  • 1
  • 3

1 Answers1

1

SetSampleResponse extension on HelpPageConfig is for statically defining samples for you action.

config.SetSampleResponse("\"Hello World!\"", new MediaTypeHeaderValue("application/json"), "Values", "Get", "id");

if you are looking for generated sample for a particular type, have you tried using SetSampleObjects extension which allows you to set sample objects for different types and this same object is used in all cases where that particular type is returned from an action.

config.SetSampleObjects(new Dictionary<Type, object>
        {
            {typeof(string), "Hello World!"}
        });

Could you share more specific(code) details as to how you are using SetSampleResponse extension?

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Thank you for the info about response being static and such. I think my problem really exists in that there is a custom configuration with a help controller instead of the default values controller and a few other tweaks in the MVC4 code where we're using the Web API. I tried SetSampleObjects in a variety of ways as well to no avail yet... Something I try is bound to work one of these days though.... Thanks again for the reply! – user1258050 Apr 26 '13 at 12:30