When and how to use the Asp.net web api

Asked

Viewed 1,027 times

2

I have been studying Asp.net MVC for some time, I have been reading about WebApi but I still can’t understand when and how I can use it.

Ex: I have a small news system, where I have a area where is the administrative part and the root project the site itself.

How could I use the WebApi in this scenario or in what scenario is most conducive to its use?

1 answer

1


I don’t see as much usability for this scenario that you mentioned, but yes it is possible to use.

If your application is a SPA (Single Page Applications), that is, it has only one page, yes it should use Webapi, because it is common in this application to use Ajax in which you will return to the customer only what is necessary, in other words, the information is already treated and I will only show the user what he requested.

In this type of application data consumption should be lower, which can also be applied to services that will be made available for mobile devices.

Returning to your scenario with the administrative module, you can do the publication of the news via Ajax, and in case would use POST and the return could be a Boolean stating: Yes the article was published (True); It was not possible to publish the article (False); in this case you would receive the data, handle in your javascript and show user friendly, all without updating the page. The possibilities would be infinite, it depends on your knowledge.

I use Webapi for data return in Json and even HTML ready, I decrease and load a lot of data.

I spoke little and you may not have understood the actual usability yet, so let’s go.

Imagine the following scenario, I have a website that provides an Api for you to retrieve information from bands. It would work as follows, the user sends me the artist/band and I return to it in json/xml information about the artist.

Controller

public class ArtistaController : ApiController {
    public Artista Get(string artista) {
       Artista artista = new Artista();      
       .... minha lógica, com modelos etc...
       return artista;
    }
}

The Request Url (GET) would be: http://meuexemplo.com/Api/Artista/Nome_do_Artista

And suppose I have the ready-made model of the artist object, that would be the return on json

My request url for the band Alexisonfire http://meuexemplo.com/Api/Artista/Alexisonfire

Return

[{"Artist: "Alexisonfire", "Genre" "Post Hardcore"}]

And finally who requested can adapt the information to its application.

Complementary tips

Return HTML in Controller without Webapi

    public ActionResult Index()
    {
        return View();
    }

    [ActionName("Index")]
    [HttpPost]
    public ActionResult PartialIndex()
    {
        return PartialView();
    }

Note that I am using the [HttpPost], that means that every time I send a Post it will return me to View Partial and not the whole View.

I hope you understand.

  • I have been using web api with WCF on REST architecture. Someone told me this is an ideal way of using web api.

  • @diego-vieira As MVC study said a little while ago and I really don’t have much knowledge yet, I want to make my applications more dynamic and friendly, for example: My administrative panel at each action gives refresh on the whole page and I’m looking for the best way to make everything more dynamic and without so much refresh, I thought wepapi could help me.

  • @Hermesautran, so can use without fear, the way is to use Webapi even if you want to remove the refresh from your system, if you want I can give a complemented ai in response with more situations with your use case, ok?

  • @diego-vieira If you can complement something more it would be very good, if you have any suggestions otherwise that would result in the same result I want will be welcome!

  • @Hermesautran, has an application that I am doing at work and for html I do in the same normal controller, has a trick for you instead of returning to View you return to partial view, when it is data (Objects, Models, lists, etc.) I use Webapi.

  • @Diegovieira Do you have an article or tutorial where I can read and study about this procedure or did you create it? If it was you, you can share it?

  • @Hermesautran I will put in the answer as an example if I do not get dirty here in the comments

Show 2 more comments

Browser other questions tagged

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