Question

I need WebApi Cors dll for web api project. I installed Web Api version for .net 4 framework, because I use VS 2010. I tried to install web api cors lib via nuget (Install-Package Microsoft.AspNet.WebApi.Cors), and this error is shown "Microsoft.AspNet.WebApi.Client 5.1.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files that are compatible with that framework". Is there workaround to install Cors for web api client 4?

Était-ce utile?

La solution 3

The CORS is supported in Web API 2 that needs .NET Framework 4.5 and visual studio/express 2014.

It is not available for Web API 1

Autres conseils

CORS is natively supported in Web API 2 only. However, you can write your own extensions. Take a look at this tutorial by Carlos Figueira. The tutorial not only applies to the RC version but also to final version 1.

You need to check EF version before inastalling above package if it is EF version 4.5 and above then it works otherwise it rollbacks below EF version 4.5

And ** Access-Control-Allow-Origin ** problem is solved no need to do extra work

and you can do it in VS 2013 web express

using System.Web.Http.Cors;
namespace WebApplication10
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

Hope this will help Thank you

This is WebApiConfig.cs file

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

and using System.Web.Http.Cors;

these are the changes you need to do. For Api Controller

public class ValuesController : ApiController
{

    // GET api/<controller>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

just add [HttpGet] attribute and all is set.

For IIS 6 servers:

To CORS-enable Microsoft IIS6, perform the following steps:

  • Open Internet Information Service (IIS) Manager
  • Right click the site you want to enable CORS for and go to Properties
  • Change to the HTTP Headers tab In the Custom HTTP headers section, click Add
  • Enter Access-Control-Allow-Origin as the header name
  • Enter * as the header value
  • Click Ok twice

http://enable-cors.org/server_iis6.html

For IIS7

Merge this into the web.config file at the root of your application / site:

  <?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </configuration>

sourced from w3

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top