0

I am pretty new to ASP.NET Website programming. I have an node js express application where I need to make requests to. This currently doesnt works from my asp.net site because i dont have cors enabled. I hope you can help me and if I am just beeing stupid and configured my website wrong or forgot to add a controller please let me know.

I tried adding cors package via nuget and adding it to the web.config.

itminus
  • 23,772
  • 2
  • 53
  • 88
Fab
  • 1
  • 1
    Possible duplicate of [How to enable cross origin requests in ASP.NET MVC](https://stackoverflow.com/questions/40079214/how-to-enable-cross-origin-requests-in-asp-net-mvc) – Kenneth K. Sep 17 '19 at 19:30
  • haven't done so myself, but i think this article may help https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api – SomeStudent Sep 17 '19 at 20:25
  • no this is not a duplicate im not having (as far as i know) no asp.net mvc website. – Fab Sep 17 '19 at 20:49
  • And the article doesnt really helps me because i think the article is for mvc – Fab Sep 17 '19 at 20:49
  • I remove the `asp.net-core` tag since it is not a asp.net-core related question – itminus Sep 18 '19 at 00:57

1 Answers1

0

In the Solution Explorer, expand the WebApi project. Open the file App_Start/WebApiConfig.cs, and add the following code to the method WebApiConfig.Register.

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

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

Then add the attribute [EnableCors] to the desired controller:

namespace MyProject.Controllers
{
    [EnableCors(origins: "http://myclient.azurewebsites.net", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
}
awais
  • 492
  • 4
  • 17
  • So i wanted to try it but i dont have a folder called App_Start. http://prntscr.com/p7u0en Thats all i got – Fab Sep 18 '19 at 15:45
  • check your files where you have registered your routes or simply search ```Routes.MapHttpRoute``` in your solution. – awais Sep 18 '19 at 15:52