Skip to content
Loading
  • Madan Shekar
    +1
    @
    Veerendra Annigere
     
    thanks for sharing valuable  information...

    My question is in case of API's are deployed at Godaddy or some other servers , in such case also we will get same configuratoion section.
    +1
  • Madan Shekar
    0
     using System.Web.Http.Cors;
    [EnableCors(origins: "*", headers: "*", methods: "*")] 
    public class LoginController : ApiController
    {
    //your code 
    } 
     
    run this nuget package manger 
    Install-Package Microsoft.AspNet.WebApi.Cors -Version 5.2.7

    If it helps pls mark as answer. 
    0
  • Veerendra Annigere
    0
    Madan Soraba.
     
    No.Configuration varies for different hosting vendors.
     
    For Godaddy,

    Include a file named .htaccess (notice the leading dot) anywhere in the path of the requested file, that file should contain this line:

    Header set Access-Control-Allow-Origin "*". 

    My point is CORS issue is nothing to do with frontend. Configuration needed in backend. 
    0
  • Bidyasagar Mishra
    0
    Go through the below link you will get the solution for 
    http://csharp-video-tutorials.blogspot.com/2016/09/cross-origin-resource-sharing-aspnet.html 
    0
  • 0
    WebConfig.cs 
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;
    using System.Web.Http.Cors;
    namespace UrbanDB.API
    {
    public static class WebApiConfig
    {
    public static void Register(HttpConfiguration config)
    {
    // Web API configuration and services
    EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*", "Access-Control-Allow-Origin");
    config.EnableCors(cors);
    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );
    }
    }
    }
     
     
    And below code just put on controller name Like
     
     [EnableCors("*","*","*")]
    public class DemoController : ApiController  
    0
  • Rajeesh Menoth
    0
    Hi,
     
    Try the following steps.
     
    1. Install CORS to the API project "Install-Package Microsoft.Asp.Net.WebApi.Cors".
    2. Open the App_Start/WebApiConfig.cs file and add the following code to the WebApiConfig.Register method.
    First enable the cors then add the remening section I think in your code I am able to see cors enable is done after the route.
    1. using System.Web.Http;    
    2.     
    3. namespace WebServiceApp    
    4. {    
    5.     public static class WebApiConfig    
    6.     {    
    7.         public static void Register(HttpConfiguration config)    
    8.         {    
    9.             //CORS config    
    10.             config.EnableCors();    
    11.             // Web API configuration and services    
    12.     
    13.             // Web API routes    
    14.             config.MapHttpAttributeRoutes();    
    15.     
    16.             config.Routes.MapHttpRoute(    
    17.                 name: "DefaultApi",    
    18.                 routeTemplate: "api/{controller}/{id}",    
    19.                 defaults: new { id = RouteParameter.Optional }    
    20.             );    
    21.         }    
    22.     }    
    23. }    
       3. Add the [EnableCors] attribute to the UserController.
     
    Only copy the " [EnableCors(origins: "http://localhost:8578", headers: "*", methods: "*")] " and orgins is nothing but the angular request url domain name. Currently we are tested in the local host so that's why put as localhost url.
    1. namespace WebServiceApp.Controllers    
    2. {    
    3.     [EnableCors(origins: "http://localhost:8578", headers: "*", methods: "*")]    
    4.     public class UserController : ApiController    
    5.     {    
    6.         public HttpResponseMessage GetInfo()    
    7.         {    
    8.             var res = new List() {    
    9.                 new User { ID = 1, Name = "Gopal", Email = "[email protected]" },    
    10.                 new User { ID = 2, Name = "Nayan", Email = "[email protected]" },    
    11.                 new User { ID = 3, Name = "Shuvo", Email = "[email protected]" }    
    12.             };    
    13.             return Request.CreateResponse(HttpStatusCode.OK, res);    
    14.         }    
    15.     }    
    16. }   
    Reference :
    https://www.c-sharpcorner.com/UploadFile/009ee3/implementation-of-cross-origin-request-in-Asp-Net-web-api-2/ 
    0