locked
how many way we can enable cors RRS feed

  • Question

  • User264732274 posted
    How to enable cors for full project and how to enable it for few action?
    Wednesday, August 31, 2016 6:45 AM

Answers

  • User1881638666 posted

    Hi stupid_inn

    You may go through this and check,

    http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

    Read the section 'Scope Rules for [EnableCors]'

    So then you can find how to enable CORS, 

    1. action level

    2. controller level

    3. globally

    Thanks & Regards,

    Wenushka

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2016 8:51 AM
  • User36583972 posted


    Hi  sudip_inn,

    How to enable cors for full project

    The following code to enable CORS across the API without annotating each controller

     public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {   
                var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
                config.EnableCors(corsAttr);
            }
        }

    how to enable it for few action?

    Firstly, you should enable CORS support in your Web API(add the Microsoft.AspNet.WebApi.Cors NuGet package to your project).

     public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {   
                //------------
                config.EnableCors();
                //-----------
            }
        }

    Then, you can add the [EnableCors] attribute to your Web API controller or controller method.

    [EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2016 4:13 AM

All replies

  • User1881638666 posted

    Hi stupid_inn

    You may go through this and check,

    http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

    Read the section 'Scope Rules for [EnableCors]'

    So then you can find how to enable CORS, 

    1. action level

    2. controller level

    3. globally

    Thanks & Regards,

    Wenushka

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2016 8:51 AM
  • User36583972 posted


    Hi  sudip_inn,

    How to enable cors for full project

    The following code to enable CORS across the API without annotating each controller

     public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {   
                var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
                config.EnableCors(corsAttr);
            }
        }

    how to enable it for few action?

    Firstly, you should enable CORS support in your Web API(add the Microsoft.AspNet.WebApi.Cors NuGet package to your project).

     public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {   
                //------------
                config.EnableCors();
                //-----------
            }
        }

    Then, you can add the [EnableCors] attribute to your Web API controller or controller method.

    [EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 1, 2016 4:13 AM