Webapi view content in a view

Asked

Viewed 440 times

1

Hey, guys, all right?

I am developing my first application in web api asp.net. And I’m having trouble displaying the content on a view.

Controller:

public class ListaPresenteController : ApiController
    {
        public IHttpActionResult Get()
        {
            var produto = new Produtos();
            return Ok(produto.Listar());
        }
    }

With this code it is working, but I could only display the list of products on home, I’m having trouble setting up the view of products, and I want to display the list of products for example in the url: www.meupai.com/produtos.

  • 1

    I believe that the idea of Web API is not well understood. Web API does not assume that the generated result will be a View, necessarily. The result is usually a JSON, interpreted by the calling page, which may or may not display a View for this.

  • Um, it may be @Ciganomorrisonmendez but here comes my question, how could I create a view, where its url is www.meuapi.com/products and I can display and manipulate (for example on a grid) the JSON’s decoration?

  • You can implement a static HTML and call the Web API methods using JSON, which is what the Web API tutorials suggest: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

1 answer

1

If your return has to be a view, I recommend using a controller, as in the example below. webApi works as a WS, hence its return and a json or xml.

public class ListaPresenteController : Controller
{

}
  • In this case instead of using Apicontroller I use a controller?

  • I believe I’m lost between the mvc controller and the webapi controller. And I intend to use a view to manipulate, display the json data returned from the webapi.

  • 1

    You can directly return the json data in your action, which will pass the data to the view or in your view call an action that receives the json data.

  • Sorry, I could not understand right, how to return the data direct in my action?

  • It would be like this: public Ilist<Products> Getproducts() { var product = new Products(); Return (product.List(); } So it would return straight to your view.

  • Thanks @Thiago.adriano26 Today I will work on this project later, then we will see the result and put here. Thanks and much for the help :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.