Creating Sample Documentation for Return Date in Web Api 2.2 Help

Hi all,

I have a Web Api 2 project which I am using the automatically generated documentation from.

One of the methods is returning a JSON string from a stored proc call and as such there is no info in the return and i get sample not available.

I have looked at the docs, but Its not clear to me. I belive I should be looking a the following;

            //// 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>));

Open in new window

CAn someone please help tell me how I would set up a sample for a specific method?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I am not sure that I understand the context for your question.  I am out in left field somewhere, and you are at home plate, so I think that we need to need to meet somewhere in the middle.
flynnyAuthor Commented:

When I run look at the generated documentation for my webapi, Under response body formats I have 'sample is not available'.

I assume this is because the method is simply returning a string.

The webmethod is;

        public IHttpActionResult Get(string username)
                var response = FrameworkRepository.GetFramework(username);
                if(response != "")
                    return Ok(response);
                    throw new HttpResponseException(HttpStatusCode.NoContent);
            catch(Exception ex)
                throw new HttpResponseException(HttpStatusCode.Conflict);

Open in new window

where the return Framework call is pulling some info from the db, converting to a Json string and returning;

public static string GetFramework(string username)
                using (var dataContext = new TelematicusEntities())
                    //calls the stored procedure
                    var framework = dataContext.GetFrameworkWeb(username);

                    //poss convert to more readable column names
                    //build into new model structure and send this way

                    var frameworkSerialized = new JavaScriptSerializer();
                    var json = frameworkSerialized.Serialize(framework);

                    return json;
            catch (Exception ex)
                return ex.Message;

Open in new window

Now is there a way I can fix the generation so that it generates a sample. Or can i physically hardcode in a sample?
Bob LearnedCommented:
Are you talking about this?

ASP.NET Web API Help Page Part 3: Advanced Help Page customizations
flynnyAuthor Commented:
HI Bob,

To a certain extent yes. I saw this but it looks a little outdated?

I just want to provide some example information on what would be returned.

Ideally i would provide a class type that it could maybe use reflection to read? (I assume this is normally how it generates the xml)?
Bob LearnedCommented:
I am trying desperately to get some context to your question, and I don't think that I am doing a good job.

I guess that search result was because of the phrase "sample is not available".

Here is the discussion below the article:

The Response Sample returns "Sample is not available" when a controller returns an interface. The ObjectGenerator returns null since there is not a default constructor. If I change the return to a concrete type the sample is generated. What is the proper method to generate the sample in this situation? Do I just need to modify the ObjectGenerator to use my DI container or am I missing something?

 Yao - MSFT 26 Jan 2013 11:50 AM #
Hi Josh,

Yes, modifying the ObjectGenerator would be the way to go. However, if you don't have too many interfaces you can consider using the config.SetSampleObjects in HelpPageConfig.cs to set the sample objects manually:

           config.SetSampleObjects(new Dictionary<Type, object>


               {typeof(IWork), new MyWork{ MyProperty=4}}


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.