【.Net Core】在.net core中调用web api并将json字符串传递给post方法(其中也包含通过ajax调用web api的方法)

前言

在很多业务场景中经常会遇到调用web api接口,并且把参数传递给web api,下面将一起了解一下如何在.net core中调用web api,并且将json字符串传递给post方法

环境和组件

环境:.net core

组件:RestSharp(可以通过NuGget组件包添加到项目组)

具体实现

1、web api方法

//注意:.net core web api支持dynamic,实例中是将请求过来的json字符串转换为了list
[HttpPost("SyncNews")]
        public async void SyncNews([FromBody] List<Article> data)
        {
            
            try
            {
                //此处是调用entity framework把数据插入到数据库
                var articleRepository = new ArticleRepository();
                await articleRepository.SyncDataAsync(data);
               
                
            }
            catch (Exception ex)
            {
                
                //此处写log日志等
            }
            
        }


//业务方法
public async Task SyncDataAsync(List<Article> articles)
        {
            using (var db = new PortalManagementContext())
            {

                foreach (var item in articles)
                {
                    db.Articles.Add(item);
                }

                await db.SaveChangesAsync();
            }
        }

 2、调用web api(Post)

下面是调用web api的两种方式

.net core请求:

//articles为Article集合
 List<Article> newaddArticleList = articles;
            var ArticleServiceUrl = RandUXML.Read("ArticleServiceUrl");
            var clientsharp = new RestClient(ArticleServiceUrl+"api/Article/SyncNews");
            clientsharp.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            //将list集合转换为json字符串,并传递给web api
            string ListJson = JsonConvert.SerializeObject(articles);
            request.AddParameter("application/json", ListJson, ParameterType.RequestBody);
            IRestResponse response = clientsharp.Execute(request);
            Console.WriteLine(response.Content);


Ajax请求:

var settings = {
  "url": weburl+"/api/Article/SyncNews",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "Content-Type": "application/json"
  },
  "data": JSON.stringify("json字符串"),
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

3、调用web api(Get)

下面是.net core及JQuery调用web api方法

.Net Core:
var client = new RestClient(weburl+"/api/Article/GetAllArticles");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

JQuery:
var settings = {
  "url": weburl+"/api/Article/GetAllArticles",
  "method": "GET",
  "timeout": 0,
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

如上就是.net core调用web api的方法,如上描述如有错漏请大家评论区直接留言,多谢

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来学吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值