Web应用调用.Net Core API

一、新建Web Application应用:

选择Web Application

新建好之后页面如下:

二、新建Model。新建Model文件夹并建立apiModel.cs。

三、添加reference。

将Newtonsoft.Json添加到项目中。因为需要将json数据转换成对象,因此需要添加reference。

四、建立呼叫API的服务。

public class callapi_service
{
public static TResult<T> getApiModels<T>(string strMethod, int id)
{
string result = string.Empty;
TResult<T> models;
string apiUrl = ""; if (strMethod=="GET")
//apiUrl += getUrlQueryString(id);
apiUrl = "http://localhost:19357/api/values/"+id;
try
{
HttpWebRequest request = HttpWebRequest.Create(apiUrl) as HttpWebRequest;
request.Method = strMethod;
//request.ContentType = httpContentType;
//request.Timeout = timeout;
//request.Headers.Add(keyHeaderAuthorization, getApiToken()); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
string strHttpStatus = response.StatusCode.ToString();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
result = sr.ReadToEnd();
}
}
models = JsonConvert.DeserializeObject<TResult<T>>(result);
}
catch (WebException ex)
{
models = default(TResult<T>);
models.rc = ;
models.msg = ex.Message;
}
catch (Exception ex)
{
models = default(TResult<T>);
models.rc = ;
models.msg = ex.Message;
}
return models;
} #region TResult
public class TResult
{
/// <summary>
/// result code
/// </summary>
public int rc { get; set; } /// <summary>
/// error message
/// </summary>
public string msg { get; set; }
}
public class TResult<T> : TResult
{
/// <summary>
/// result code
/// </summary>
public int rc { get; set; } /// <summary>
/// error message
/// </summary>
public string msg { get; set; }
public T data { get; set; } }
#endregion
}

五、修改页面Default.aspx。

六、处理后台代码类Default.aspx.cs. 

protected void Page_Load(object sender, EventArgs e)
{
var result=callapi_service.getApiModels<List<apiModel>>("GET",);
gvApprovalList.DataSource = result.data;
gvApprovalList.DataBind();
}

七、成功

Web应用调用.Net Core API的更多相关文章

  1. web开发调用百度地图API + AK申请

    web开发调用百度地图API + AK申请 要使用百度地图的API我们首先需要在我们的html页面引入js----`` 如何获取百度地图ak: 1.登陆百度地图开发者平台 2.注册百度开发者账号并登陆 ...

  2. List多个字段标识过滤 IIS发布.net core mvc web站点 ASP.NET Core 实战:构建带有版本控制的 API 接口 ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目 Using AutoFac

    List多个字段标识过滤 class Program{  public static void Main(string[] args) { List<T> list = new List& ...

  3. web端高德地图javascript API的调用

    [转载https://www.cnblogs.com/zimuzimu/p/6202244.html]web端高德地图javascript API的调用 关于第三放地图的使用,腾讯.百度.高德 具体怎 ...

  4. ASP.NET Core API 版本控制

    几天前,我和我的朋友们使用 ASP.NET Core 开发了一个API ,使用的是GET方式,将一些数据返回到客户端 APP.我们在前端进行了分页,意味着我们将所有数据发送给客户端,然后进行一些dat ...

  5. 【从零开始搭建自己的.NET Core Api框架】(七)授权认证进阶篇

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  6. 【从零开始搭建自己的.NET Core Api框架】(一)创建项目并集成swagger:1.1 创建

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  7. 【从零开始搭建自己的.NET Core Api框架】(四)实战!带你半个小时实现接口的JWT授权验证

    系列目录 一.  创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...

  8. 【转】ASP.NET Core API 版本控制

    几天前,我和我的朋友们使用 ASP.NET Core 开发了一个API ,使用的是GET方式,将一些数据返回到客户端 APP.我们在前端进行了分页,意味着我们将所有数据发送给客户端,然后进行一些dat ...

  9. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

随机推荐

  1. Linux shell简单创建用户脚本

    前面介绍简单的shell编写规则. 现在开始编写一个简单的shell脚本. Linux shell介绍 编写shell脚本    1.创建脚本文件    2.根据需求,编写脚本    3.测试执行脚本 ...

  2. springboot中配置urlrewrite实现url伪静态强化网站seo

    关于urlrewrite urlrewrite使用强大的自定义规则来使用用户更容易记住.搜索引擎更容易找到的URL(对于seo比较重要).通过使用规则模板.重写映射,Web管理员可以轻松地设置规则,根 ...

  3. 朱石景 201671010457 团队项目评审&课程学习总结

    项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p/ ...

  4. JS 中的 new 操作符

    按照javascript语言精粹中所说,如果在一个函数前面带上new来调用该函数,那么将创建一个隐藏连接到该函数的prototype成员的新对象,同时this将被绑定到那个新对象上.这个话很抽象,我想 ...

  5. hdu1873-看病要排队-(结构体优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1873 #include<stdio.h> #include<iostream> #inc ...

  6. Active Ball

    Active Ball is a simple game. All you need to do is aim at the food and shoot it, then collect the m ...

  7. LeetCode 1034. Coloring A Border

    原题链接在这里:https://leetcode.com/problems/coloring-a-border/ 题目: Given a 2-dimensional grid of integers, ...

  8. LeetCode 691. Stickers to Spell Word

    原题链接在这里:https://leetcode.com/problems/stickers-to-spell-word/ 题目: We are given N different types of ...

  9. 元素的alt和title有什么异同?

    ①alt作为图片的替代文字出现,title作为图片的解释文字出现. ②alt属性应用较少,如img.area.input中,title应用较多,如a.form.input.还有div.p这些块级元素都 ...

  10. Unity开发:5.0+版本标准资源包无内置问题

    一.问题如下: 在Unity中,一般都会内置有基础的资源包,可以在Assets->Import Package中,点击其下的子项进行导入: 但是我发现,5.0版本与之前的4.x版本相比,安装包变 ...