I am trying to use CORS support with Web API, and have installed RC1. I have wasted so much time trying to get CORS to work correctly. Anyhow I have enabled Nightly builds with no avail. I keep getting the following issue and cannot find a solution anywhere on the net. Do you need 2013 VS to play with MVC 5 or install anything new?
Could not load type 'System.Web.Http.Hosting.IHostPrincipalService' from assembly 'System.Web.Http, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
This is my packages file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.4.1.9004" targetFramework="net45" />
<package id="AutoMapper" version="3.0.0-ci1043" targetFramework="net45" />
<package id="AutoMapper" version="3.0.0" targetFramework="net45" />
<package id="EntityFramework" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Cors" version="5.1.0-alpha1-131016" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.Providers.Core" version="1.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Providers.LocalDB" version="1.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.1.0-alpha1-131016" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.1.0-alpha1-131016" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.1.0-alpha1-131016" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="5.0.6" targetFramework="net45" />
<package id="NodaTime" version="1.1.1" targetFramework="net45" />
<package id="System.Spatial" version="5.6.0" targetFramework="net45" />
<package id="WebGrease" version="1.1.0" targetFramework="net45" />
<package id="WebGrease" version="1.5.2" targetFramework="net45" />
</packages>
There are also other ways of enabling CORS on the server-side, other than using the ASP.NET CORS Nuget package. You can add the CORS headers to the response manually (e.g. by writing a custom Web API MessageHandler, or using some other code mechanism to add a header to the response), or you can add headers using the web.config:
Using the CORS Nuget package is probably the recommended approach (once the 5.1+ version is actually released), but it is not the only way.
I've gotten these alternative methods to work in VS2012 using MVC4 and the VS2012-era Web API version.
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