5

I created one self-hosted Web API in asp.net it works fine when I call it from POSTMAN but it gives below error when I invoke it from browser.

Access to XMLHttpRequest at 'http://localhost:3273/Values/GetString/1' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-A

Below given is my service class

using System.Web.Http;
using System.Web.Http.SelfHost;

namespace SFLSolidWorkAPI
{
    public partial class SolidWorkService : ServiceBase
    {
        public SolidWorkService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            var config = new HttpSelfHostConfiguration("http://localhost:8069");


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

            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
        }

        protected override void OnStop()
        {
        }
    }

}
Bhaumik Thakkar
  • 580
  • 1
  • 9
  • 28
KEVAL PANCHAL
  • 535
  • 3
  • 14
  • 1
    Have you try to use Microsoft.AspNet.WebApi.Cors? Docs https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api – abestrad Feb 09 '19 at 15:33
  • Yes I try but it not work for me Even I try to Enable cros on controller level but it also not work – KEVAL PANCHAL Feb 09 '19 at 16:27
  • Use a sniffer like wireshark or fiddler. Compare working Postman results with non working app. Modify app so http headers are the same as Postman. – jdweng Feb 09 '19 at 17:24
  • Make sure your service is setting the following headers, "Access-Control-Allow-Origin", "*" "Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With" "Access-Control-Allow-Methods", "GET, PUT, POST" – abestrad Feb 10 '19 at 14:43
  • Add added `[EnableCors(origins: "*", headers: "*", methods: "*")]` on controller level but still it not working. – KEVAL PANCHAL Feb 11 '19 at 07:58

1 Answers1

8

Here,

I found solution for this problem after so many research. You just need to install Microsoft.AspNet.WebApi.Cors and use it like config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));

Hope it will help others.

Thanks

using System;
using System.ServiceProcess;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.SelfHost;

namespace SFLSolidWorkAPI
{
    public partial class DemoService : ServiceBase
    {
        public DemoService ()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            var config = new HttpSelfHostConfiguration("http://localhost:8069");


            config.Routes.MapHttpRoute(
               name: "API",
               routeTemplate: "{controller}/{action}/{id}",
               defaults: new { id = RouteParameter.Optional }
           );
            config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));

            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();
        }

        protected override void OnStop()
        {
        }
    }

}
KEVAL PANCHAL
  • 535
  • 3
  • 14