首页 > 其他分享 >.Net Core Razor Page添加WebApi

.Net Core Razor Page添加WebApi

时间:2022-10-12 18:36:14浏览次数:44  
标签:WebApi Core string Razor System api services using endpoints

 

1、在 Startup.cs 的函数 ConfigureServices 添加 services.AddControllers();

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

            services.AddControllers();
        }

 

2、在 Startup.cs 的函数 Configure 添加  endpoints.MapControllers();

app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();


                endpoints.MapControllers();
            });

3、新建文件夹api

4、新建api:newsController

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Web1.Api
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class newsController : ControllerBase
    {
        public string GetData1(string id, string name)
        {
            string res = "id=" + id + ",name=" + name;
            return res;
        }
    }
}

webapi写法和mvc中的一样

 

标签:WebApi,Core,string,Razor,System,api,services,using,endpoints
From: https://www.cnblogs.com/webapi/p/16785534.html

相关文章

  • 【Q&A】Abp WebApi [FromBody] 简单类型渡劫Axios请求
    后端接口强制WebAPI从请求正文读取简单类型,添加[FromBody]属性到参数[HttpPost][Route("create")]publicTask<CalendarDto>CreateAsync([FromBody]......
  • 【GAN优化】最早被用于评价GAN模型的定量指标Inception Score是什么
    最近一部分的内容将会比较容易,将和大家一起讨论GAN的评价指标,也没有太难以理解的东西,希望大家踊跃讨论,欢迎留言。作者&编辑|小米粥编辑|言有三在判别模型中,训练完成的模......
  • [2core]基于httpclient实现负载均衡
    一、准备由于没有采用asp.netcore框架提供的HttpClientFactory和依赖注入方式使用HttpClient,所以从asp.net迁移到asp.netcore变化并不大,而且HttpClient的使用也基本没有......
  • 给dotnet core web api swagger文档加注释
    API方法中添加注释 2.指定注释文档路径3.项目配置生成选项卡中输出项沟选XML文档文件并指定输出路径4.运行项目,此时可看到Swagger文档中API注释 ......
  • CSharp: Chain of Responsibility Pattern in donet core 3
     ///<summary>///责任链模式ChainofResponsibilityPattern亦称:职责链模式、命令链、CoR、ChainofCommand、ChainofResponsibility///geovindu,......
  • 使用CoreDNS自建dns
    前言公司有些内网服务需要使用域名访问,安装bind比较麻烦,故使用coredns实现域名服务。IP说明192.168.0.41安装dns,作为dns服务器192.168.0.20测试服务器......
  • 开箱即用~基于.NET Core的统一应用逻辑分层框架设计
    目前公司系统多个应用分层结构各不相同,给运维和未来的开发带来了巨大的成本,分层架构看似很简单,但保证整个研发中心都使用统一的分层架构就不容易了。那么如何保证整个研......
  • DataCore PlgIn for vCenter 3.0 SETTINGs
    第1章 VMwarevSphere3.0Plug-In只需要4步骤,可以在vCenterClient配置一颗存储,可具备高可用性,Multipath-RoundRobin,HighCaching...etc是不是很有趣。1.1 概述VMware......
  • NETCORE中如何操作Appsettings.json 文件
    对于很多初学NETCORE的同学来说,怎么从appsettings.json文件中获取各种类型数据,一直没搞明白。今天我们就对它的几种数据格式的读取做个说明。appsettings.json 是我们......
  • AspNetCore中 使用 Grpc 简单Demo
    为什么要用Grpc跨语言进行,调用服务,获取跨服务器调用等目前我的需要使用我的抓取端是go写的查询端用Net6写的导致很多时候我需要把一些临时数据写入到Redis在两......