locked
API Documentation unavailable RRS feed

  • Question

  • User1489758560 posted

    Hi,

    I have created a asp.net web api and i am all set for making it to work. by default it has account,values,home when i create the project. i tried to add the new controller as below,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using RestAPI.Models;
    namespace RestAPI.Controllers
    {
        [Authorize]
        [RoutePrefix("api/Product")]
        public class ProductsController : ApiController
        {
            Product[] products = new Product[] 
            { 
                new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1m }, 
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75m }, 
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99m } 
            };
    
            public IEnumerable<Product> GetAllProducts()
            {
                return products;
            }
    
            //[Route("{id:int}")]
            [Route("GetProduct/{Id}")]
            public IHttpActionResult GetProduct(int Id)
            {
                var product = products.FirstOrDefault((p) => p.Id == Id);
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
    
           
            [Route("GetProductByName/{Name}")]
            public IHttpActionResult GetProductByName(string Name)
            {
                var product = products.FirstOrDefault((p) => p.Name == Name);
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
    
    
            //[Route("{Name:alpha}/{ID:int}")]
            [Route("GetProductByName/{Name}/{ID}")]
            public IHttpActionResult GetProductByName(string Name, int ID)
            {
                var product = products.FirstOrDefault((p) => (p.Name == Name && p.Id == ID));
                if (product == null)
                {
                    return NotFound();
                }
                return Ok(product);
            }
        }
    }
    

    When build and run the project it launches the page localhost:4033/help. the default controllers created by VS has the documentation avaialble. where as the controller which i created doesn't have documentation. Very wired

    No documentation available.
    
    Request Information
    Parameters
    Name	Description	Additional information
    Id	
    No documentation available.
    Define this parameter in the request URI.
    Response Information
    No documentation available.
    
    Response body formats
    application/json, text/json, text/xml
    Sample:
    Sample not available.
    
    © 2015 - My ASP.NET Application

    I tried to Google and even i followed the below link, but no hope.

    https://coderwall.com/p/sqmrog/3-easy-steps-to-create-webapi-documentations

    Any suggestion please

    Tuesday, May 12, 2015 9:16 PM

Answers

  • User1489758560 posted

    i figured out by myself

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2015 6:18 PM

All replies

  • User1489758560 posted

    i figured out by myself

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2015 6:18 PM
  • User1644755831 posted

    Hello born2win,

    We are glad that you are able to resolve your problem. It would be beneficial for other community members if you could post the solution to the problem.

    With Regards,

    Krunal Parekh

    Thursday, May 14, 2015 5:31 AM
  • User631899493 posted

    This happens when the response type is HttpResponseMessage or any anonymous types like dynamic/JToken.

    Add ResponseType attribute with the Dto type to the Web method

    example:

    // GET api/Books/5
            [ResponseType(typeof(BookDto))]
            public async Task<IHttpActionResult> GetBook(int id)
            {
                BookDto book = await db.Books.Include(b => b.Author)
                    .Where(b => b.BookId == id)
                    .Select(AsBookDto)
                    .FirstOrDefaultAsync();
                if (book == null)
                {
                    return NotFound();
                }
    
                return Ok(book);
            }
    
    
    


    Prasanjeet Debnath

    Thursday, May 14, 2015 10:07 AM
  • User1489758560 posted

    Sorry Krunal, i didn't see your reply. Apologize for late reply. The answer already posted by  pjeet_4411

    Saturday, May 23, 2015 11:05 PM