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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With