.Net Core Razor Page添加WebApi

 

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中的一样

 

posted @ 2022-10-12 18:21  WebApi  阅读(362)  评论(0编辑  收藏  举报
CopyRight © 博客园 WebAPI