Enable CORS In ASP.NET Web API



How to Enable cross-origin requests in ASP.NET Web API 2? 1. Create the WebService project create a new ASP.NET Web Application (.NET Framework) project 2. Select the Empty project template and select the Web API checkbox 3. Add a Web API controller 4. You can run the application locally to check your API call is Successfully called or not 5. Create another ASP.NET Web Application (.NET Framework) project and select the MVC project template select Change Authentication - No Authentication 6. open the file Views/Home/Index.cshtml(Go to view of index from Home Controller) Remove all code and add one lable and button to this page and write ajax call to call Method from Api Project 7. Set MVC project as Startup Project run Project when I click on Submit Button it shows 405 error(Method Not Allowed) in browser debugging mode 8. Enable CORS First, add the CORS NuGet package from the Tools menu, select NuGet Package Manager and then select Package Manager Console confirm Default project name API Project is selected type the command: Install-Package Microsoft.AspNet.WebApi.Cors 9. Open the file App_Start/WebApiConfig.cs from API Application Add the following code to the WebApiConfig.Register method config.EnableCors(); 10. Add the namespace using System.Web.Http.Cors; and [EnableCors] attribute to the Api Controller [EnableCors(origins: "*", headers: "*", methods: "*")] This allows cross-origin requests from Client Application #Web #API #Restful #Service #RestAPI #core #API2

Post a Comment

Previous Post Next Post