WebApi cors for runtime 4.0

前端 未结 5 1654
别跟我提以往
别跟我提以往 2021-01-21 23:36

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-Packa

5条回答
  •  天涯浪人
    2021-01-22 00:31

    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/
        [HttpGet]
        public IEnumerable Get()
        {
            return new string[] { "value1", "value2" };
        }
    

    just add [HttpGet] attribute and all is set.

提交回复
热议问题