创建Web API并使用
昨晚有教一个网友在ASP.NET MVC里,创建Web API和在MVC视图中应用此API。
可以在ASP.NET MVC中,创建程序的model:
namespace Insus.NET.Models
{
public class Weather
{
private int _Month; public int Month
{
get { return _Month; }
set { _Month = value; }
} private string _Season; public string Season
{
get
{
return _Season;
}
set { _Season = value; }
}
}
}
Source Code
model创建好了,可以开始创Web API:
如果你的环境是第一次创建Web API的,它会打开一个文本,是教诉你怎样配置Global.asax.cs的:
稍后注意一下,api的路由注册应该放在mvc的路由前面。
接下来,为你的api接口写方法或是函数:
namespace Insus.NET.Apis
{
public class WeatherAPIController : ApiController
{
[Route("api/WeatherAPI/ApiMethod")]
[HttpPost]
public Weather ApiMethod(Weather w)
{
var a = new List<int>() { , , };
if (a.Contains(w.Month))
w.Season = "春季"; var b = new int[] { , , };
if (b.Contains(w.Month))
w.Season = "夏季"; IEnumerable<int> c = new List<int>() { , , };
if (c.Contains(w.Month))
w.Season = "秋季"; var d = "10,11,12".Split(new char[] { ',' }).Select(i => Int32.Parse(i)).ToArray();
if (d.Contains(w.Month))
w.Season = "冬季"; return w;
}
}
}
Source Code
以上内容全是Web API的建设。前端介面,就可以使用它们了。
现在ASP.NET MVC网站上,添加一个视图,并添加一些html:
为铵钮补充click事件,那是使用jQuery的ajax来POST用户输入的月份,然后系统返回季度:
$(function () {
$("#btnConvert").click(function () {
var obj = {};
obj.Month = $("#txtMonth").val();
$.ajax({
type: "POST",
url: "/api/WeatherAPI/ApiMethod",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('.label').html("你输入的月份:" + response.Month + "; 转换季度是:" + response.Season + "。");
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
Source Code
演示:
创建Web API并使用的更多相关文章
- ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API
ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...
- 如何在Mvc 6 中创建 Web Api以及如何脱离IIS实现自我托管
微软推出的Asp.net vNext(asp.net 5.0)的其中的一个目标就是统一mvc 和web api 的框架.接下来我就演示一下一下几个内容 1,怎么在Asp.net mvc 6 中创建简单 ...
- 【ASP.NET Web API教程】2.4 创建Web API的帮助页面
原文:[ASP.NET Web API教程]2.4 创建Web API的帮助页面 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 ...
- 在 MVC6 中创建 Web API
ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ...
- Mvc 6 中创建 Web Api
如何在Mvc 6 中创建 Web Api以及如何脱离IIS实现自我托管 微软推出的Asp.net vNext(asp.net 5.0)的其中的一个目标就是统一mvc 和web api 的框架.接下来我 ...
- 使用 ASP.NET Core MVC 创建 Web API(五)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使 ...
- 使用 ASP.NET Core MVC 创建 Web API(一)
从今天开始来学习如何在 ASP.NET Core 中构建 Web API 以及每项功能的最佳适用场景.关于此次示例的数据库创建请参考<学习ASP.NET Core Razor 编程系列一> ...
- 使用 ASP.NET Core MVC 创建 Web API(二)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...
- 使用 ASP.NET Core MVC 创建 Web API(三)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 十 ...
- 使用 ASP.NET Core MVC 创建 Web API(四)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 使用 ASP.NET Core MVC 创建 Web API(二) 使 ...
随机推荐
- filter(ele)过滤数组
filter也是一个常用的操作,它用于把Array的某些元素过滤掉,然后返回剩下的元素. 例如,在一个Array中,删掉偶数,只保留奇数,可以这么写: function remove(arr) { l ...
- Linux 学习笔记之超详细基础linux命令 Part 2
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 1----------------- ...
- Java String和Date的转换
String—>Date方法一: String dateString = "2016-01-08"; try { SimpleDateFormat sdf = new Sim ...
- Echarts地图展示及属性分析
Echarts,一个效果非常棒的可视化库,可以生产各种图表,动态展示,附上官方网址:http://www.echartsjs.com/index.html 之前带本科实习时有同学用过,狗哥的博客也用这 ...
- format 代码
#!/bin/bash#file format.sh#将某个源代码文件中的全角符号转换成半角符号#调用astyle 将某个源代码文件转换成linux风格,缩进方式为4个空格 echo $1#将全角空格 ...
- [MapReduce_5] MapReduce 中的 Combiner 组件应用
0. 说明 Combiner 介绍 && 在 MapReduce 中的应用 1. 介绍 Combiner: Map 端的 Reduce,有自己的使用场景 在相同 Key 过多的情况下 ...
- Centos7下gogs数据配置迁移
工作需要把测试版的gogs迁移到正式版gogs,顺便记下笔记防止以后需要忘记了 环境: centos7测试版gogs:192.168.1.63 centos7正式版gogs:192.168.0.100 ...
- Django之ORM查询
ORM 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 <------->类实例对象图书管理系统的增删改查:代码如下:views ...
- springboot整合redisson分布式锁
一.通过maven引入redisson的jar包 <dependency> <groupId>org.redisson</groupId> <artifact ...
- android Application Component研究之Activity(一)
http://blog.csdn.net/windskier/article/details/7096521 终于下定决心写写ActivityManagerService的源码分析的文章了,Activ ...