------------------程序做得再好,数据有问题照样是个死 !

MVCWebapi

WEBAPI:仅仅提供了一个轻量级的Http请求响应框架 返回的结果在

MVCWebapi中有两种:1,xml 2,json

3、MVC WebApi
1、webapi的路由规则注册在App_Start\WebApiConfig.cs文件中
2、webapi控制器继承父类 apiController
3、webapi会自动的将返回结果默认以xml格式返回
4、webapi默认是根据http请求方法去控制器中查找有这个方法前缀的方法执行
例如:get请求,则查找带有Get前缀的方法,以参数来辨别要调用的具体方法是谁
5、想要webapi以json格式返回怎么办:
由于默认是以XML格式返回,那么只要将xml格式移除即可
将webapi默认的返回格式设置成json格式写法
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//将webapi中的XmlFormatter 移除,默认就是以JsonFormatter作为其传输格式
config.Formatters.Remove(config.Formatters.XmlFormatter);
}

3、调用webapi的方式:
get请求http://localhost/api/values 则默认请求 values控制器下的Get()方法
get请求http://localhost/api/values/1 则默认请求 values控制器下的Get(int id)方法将1作为id参数值传递给Get方法
Post请求http://localhost/api/values 则默认请求 values控制器下的Post方法


4、在另外一个网站请求使用httpwebrequest 请求webapi示例:
//模拟浏览器请求http://localhost:55749/api/values/GetPig 传入指定的id参数值
string requestUrl = "http://localhost:15405/infos.ashx?id=" + txtid.Text;
//1.0 实例化web请求对象的实例
WebRequest request = WebRequest.Create(requestUrl);
//2.0 设置其请求方式为get请求
request.Method = "get";
//3.0 获取服务器响应回来的响应报文对象
WebResponse response = request.GetResponse();
System.IO.Stream str = response.GetResponseStream();

//将流转换成字符串
string responsebody = "";
using (System.IO.StreamReader srd = new System.IO.StreamReader(str))
{
//将响应报文体中的数据转换成字符串
responsebody=srd.ReadToEnd();
}

Response.Write(responsebody);

--------------------


/// <summary>
/// 特点:
/// 1、必须继承ApiController
/// 2、如果想要访问webapi则必须注册webapi的路由规则(网站建好以后自动注册)
/// 3、访问mvc webapi 是通过当前http请求的方法来查找对应的action的(只要前缀匹配get或者post即可)
/// </summary>



webapi 没有视图 人家访问你 你返回一个视图起什么作用呢 所以只返回xml 和json

----------------------------
把返回格式变成json 格式

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//将xml格式移除则就是json格式返回
config.Formatters.Remove(config.Formatters.XmlFormatter);

-------------------
建 webapi

先建 MVC 然后选择 webapi

api 里 也可以有普通的控制器

1, 建一个普通控制器 然后 添加视图 构造一个form表单

------------------------
客户端 Client aspx页面 后台代码
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="callWebApi.aspx.cs" Inherits="Client.callWebApi" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
--------------------

先 using System.Net;

using System.Net;

public partial class callWebApi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
/// <summary>
/// 请求 http://localhost:35681/api/values/GetMenus 获取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//1.0 构造一个请求对象
string webapi = "http://localhost:35681/api/values/GetMenus";
WebRequest request = WebRequest.Create(webapi);
request.Method = "get";

//2.0 发出请求获取服务器的响应
WebResponse response = request.GetResponse();
//获取响应报文体中的数据
System.IO.Stream stream = response.GetResponseStream();

//3.0 将stream中的流转换成UFT-8格式字符串
string resultString = "";
using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
{
//将sr中的byte字节转换成字符串
resultString = sr.ReadToEnd();
}

//4.0 resultString:是一个json格式
//[{"Name":"西兰花炒肉","imgPath":"/imags/xlhcr.jpg","price":28.0},{"Name":"黄瓜炒冬瓜","imgPath":"/imags/hg.jpg","price":18.0}]
//4.0.1 将json字符串反序列化成List<TestModel>
System.Web.Script.Serialization.JavaScriptSerializer jsoner = new System.Web.Script.Serialization.JavaScriptSerializer();

List<TestModel> list = jsoner.Deserialize<List<TestModel>>(resultString);

//操作xml
//System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(List<TestModel>));
//xml.Deserialize(
//System.Xml.XmlDocument document = new System.Xml.XmlDocument();
//document.LoadXml("");

//jquery $("<?xml><item><id>10</id></item></xml>").find("item").find("id").val();

//5.0 将list集合绑定到GridView上
this.GridView1.DataSource = list;
this.GridView1.DataBind();
}
}
}

MVCWebapi:
1,如果数据是由我提供

应该开一个MVCWebapi,此系统中具体有哪些url

应该根据业务逻辑来解决

2,如果调用人家的WebApi呢?

则使用 WebRequest和WebResponse 进行 请求响应获取远程的数据

以后进行处理即可

posted @ 2015-03-14 23:38  俊落笔如歌  阅读(284)  评论(0编辑  收藏  举报
           人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!       ---------俊落笔如歌