Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does IAppbuilder.UseWebApi do?

I've recently been working on an MVC site that has an api and in the startup.cs there is a line that says app.UseWebApi. I did some searching but I couldn't find a decent answer of what it does. Can someone please explain the concept for me? Thanks!

like image 776
David Carek Avatar asked Jul 11 '16 14:07

David Carek


2 Answers

It configures ASP.NET Web API to run on top of OWIN. OWIN abstracts a web server and you can run it on top of both IIS as well as HTTP.SYS which allows you to provide a web server in your own console application. To be more specific the piece that runs on top of IIS or HTTP.SYS is Katana which is an implementation of the OWIN specification.

By calling app.UseWebApi you configure OWIN/Katana to send web requests through ASP.NET Web Api that in OWIN terminology is considered a middleware. This requires the NuGet package Microsoft.AspNet.WebApi.Owin.

Interestingly, ASP.NET MVC 5 cannot be configured as an OWIN/Katana middleware. it depends on System.Web and expects the HttpContext singleton to exist so it has to run on top of IIS. However, several NuGet packages that can be used in MVC projects (e.g. for authentication) is built on top of OWIN instead of taking a dependency on HttpContext which makes them more useful. That is one explanation of why you see OWIN used in a MVC project.

like image 98
Martin Liversage Avatar answered Sep 27 '22 22:09

Martin Liversage


You need to install Microsoft ASP.NET Web API2 OWIN Self Host which will resolve the issue.

You can use the following NuGet command shown below:

Install-Package Microsoft.AspNet.WebApi.OwinSelfHost 
like image 28
Alexander Svetly Avatar answered Sep 27 '22 22:09

Alexander Svetly