how to call asp.net core web api in another asp.net(web form) project.
Loading
how to call asp.net core web api in another asp.net(web form) project.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Sep 20, 2022, 9:39 AM EDT
Please refer below link
https://www.c-sharpcorner.com/blogs/how-to-call-web-api-in-another-project-from-c-sharp
Sachin SinghRank #66 · 26.2K reputationPosted Sep 20, 2022, 1:34 PM EDT
calling API from one project (Localhost:1234 or Domain A) to another project (localhost:3456 or DomainB) violates the CORS policy, so first step should be to enable CORS in your API project.
After enabling the CORS you can use HttpClient or RestClient or Jquery Ajax to consume the API from any project.
This is how you can consume API from web forms
function getProducts() { $.getJSON("api/products", function (data) { $('#products').empty(); // Clear the table body. // Loop through the list of products. $.each(data, function (key, val) { // Add a table row for the product. var row = '' + val.Name + '' + val.Price + ''; $('', { html: row }) // Append the name. .appendTo($('#products')); }); }); } $(document).ready(getProducts);To enable CORS in ASP.NET Core API
//inside startup.cs (till .NET 5) or Inside Main() in (.NET 6 and .NET 7) //inside configure method app.UseCors("CorsApi"); //inside configureServices method services.AddCors(options => { options.AddPolicy("CorsApi", builder => builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod()); });Naresh BeniwalRank #365 · 6.8K reputationPosted Oct 31, 2022, 12:28 AM EDT
https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms
Amit MohantyRank #112 · 20K reputationPosted Sep 20, 2022, 8:34 AM EDT
You can use RestSharp which is an open-source HTTP Client library that we can use to consume APIs.
For more details plese check the below articles
https://stackoverflow.com/questions/10226089/restsharp-simple-complete-example
https://code-maze.com/aspnetcore-using-restsharp-consume-api/