简体   繁体   English

如何将带有Razor页面的ASP .NET WEB APP变成API?

[英]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).所以,基本上我有一个博客网站,我使用 ASP .NET 和 Razor 页面开发,但现在我想在我的个人投资组合网站上实现这些博客文章(使用简单的 HTML、CSS 和 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.我的第一个想法是:哎呀,我现在需要从头开始创建一个 ASP .NET API 以连接到同一个博客数据库,然后使用 JS 从我的投资组合网站获取所有内容。

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?问题:我真的需要为此创建一个全新的 API 吗?或者有什么方法可以将现有博客变成一个可以从外部调用的 API 吗? Or somehow add an API endpoint that instead of rendering the pages just sends the json data back to the client?或者以某种方式添加一个 API 端点,而不是呈现页面,只是将 json 数据发送回客户端?

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将 Web API controller 添加到您现有的应用程序并使用它返回 JSON
  2. Add a named handler method to an existing PageModel class that returns a JsonResult and call that from your AJAX method命名处理程序方法添加到返回 JsonResult 的现有 PageModel JsonResult并从您的 AJAX 方法中调用它
  3. If you are using .NET 6, add a minimal API end point to your application如果您使用的是 .NET 6,请向您的应用程序添加一个最小的 API 端点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 API 和 razor 页面启动 ASP.NET Core Web 项目 - Starting an ASP.NET Core web project with API and razor pages 如何使用 Razor 页面从 ASP.net 核心简单 web 应用程序中的配置读取 json 对象数组 - How to read array of json objects from configuration in ASP.net Core simple web app using Razor pages 在 ASP.NET Core 3.1 MVC+Razor Pages+Web API 中设置默认页面 - Set default page in ASP.NET Core 3.1 MVC+Razor Pages+Web API 无法在选择列表中保存多个选择 - asp.net core web app razor pages - Can't save multiple selections in select list - asp.net core web app razor pages ASP.NET Core 2.0 Razor Pages Web应用程序单独的复杂BindProperty表单 - ASP.NET Core 2.0 Razor Pages web app separate Complex BindProperty Form asp.net core 2.0 dotvvm vs razor pages - 对于交互式 Web 应用程序,哪一个更强大? - asp.net core 2.0 dotvvm vs razor pages - which one is more powerful for an interactive web app? Asp.Net-Core 与 Razor 页面 web 应用程序在 Post 方法上发送值 - Asp.Net-Core with Razor Pages web app send values on Post method ASP.NET Core API without razor pages, using separate web app for Front-End and Anti-Forgery tokens - does it make sense? - ASP.NET Core API without razor pages, using separate web app for Front-End and Anti-Forgery tokens - does it make sense? 如何在Razor Pages Web应用程序中使用ViewModel - How to use ViewModel in a Razor Pages web app ASP.NET网页(Razor)中的验证是否已弃用? - Validation in ASP.NET Web Pages (Razor) deprecated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM