Prerequisites
Visual Studio 2017 Community, Professional, or Enterprise Edition
Step 1: Add following namespaces in your project.
---------------------------------------
Step 2: Add following code to your WebApiConfig. to enabled Cors attribute.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
namespace WebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
// Attribute routing.
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
var enableCorsAttribute = new EnableCorsAttribute("*",
"Origin, Content-Type, Accept",
"GET, PUT, POST, DELETE, OPTIONS");
config.EnableCors(enableCorsAttribute);
}
}
}
-------------------------------------------
Step 3: Update this configuration code to the 'Global.asax' file.
// Attribute routing.
}
-------------------------------------------
Step 3: Update this configuration code to the 'Global.asax' file.
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
For more information, Please click here...
No comments:
Post a Comment