2
CORS issue is nothing to do with you're coding. Configure CORS where you're API is deployed. You can set origin in you're code.
Eg: If You're API is deployed in Azure
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.
0
Hi,
Try the following steps.
- Install CORS to the API project "Install-Package Microsoft.Asp.Net.WebApi.Cors".
- 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.
- using System.Web.Http;
-
- namespace WebServiceApp
- {
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
-
- config.EnableCors();
-
-
-
- config.MapHttpAttributeRoutes();
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- }
- }
- }
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.
- namespace WebServiceApp.Controllers
- {
- [EnableCors(origins: "http://localhost:8578", headers: "*", methods: "*")]
- public class UserController : ApiController
- {
- public HttpResponseMessage GetInfo()
- {
- var res = new List<User>() {
- new User { ID = 1, Name = "Gopal", Email = "gopal@bitmascot.com" },
- new User { ID = 2, Name = "Nayan", Email = "nayan@live.com" },
- new User { ID = 3, Name = "Shuvo", Email = "shuvo@live.com" }
- };
- return Request.CreateResponse(HttpStatusCode.OK, res);
- }
- }
- }
Reference :https://www.c-sharpcorner.com/UploadFile/009ee3/implementation-of-cross-origin-request-in-Asp-Net-web-api-2/

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