简体   繁体   中英

How to turn ASP .NET WEB APP with Razor Pages into an API?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM