Home > Software engineering >  How to turn ASP .NET WEB APP with Razor Pages into an API?
How to turn ASP .NET WEB APP with Razor Pages into an API?

Time:05-05

So, basically I have a blog website that I developed using ASP .NET with Razor Pages but, now I want to implement those blog posts on my personal portfolio website (Built with simple HTML, CSS and JS).

My first thought was: Gee, I now need to create an ASP .NET API from scratch to connect to the same blog database and then fetch it all with JS from my Portfolio Website.

Question: Would I actually need to create a brand new API just for that or is there any way I can turn the existing blog into an API that can be called externally? Or somehow add an API endpoint that instead of rendering the pages just sends the json data back to the client?

Thanks you all in advance

CodePudding user response:

You have three choices:

  1. Add a Web API controller to your existing application and use that to return JSON
  2. Add a named handler method to an existing PageModel class that returns a JsonResult and call that from your AJAX method
  3. If you are using .NET 6, add a minimal API end point to your application
  • Related