ASP.NET Web API 实例
2020-12-13 03:27
标签:style class blog code http tar 创建Web API解决方案,命名为VCoinWebApi,并且创建了同名的Project,然后,创建一个Empty Project:Models,创建一个WPF Application Project:VCoinWebTester 在Models项目下,添加User类。User类的代码如下: 在VCoinWebApi项目下的Controllers文件夹上右键添加Web API 2 Controller - Empty。新建的UsersController代码如下: 修改App_Start文件夹下的WebApiConfig.cs文件的默认的路由规则,如下: 在VCoinWebTester项目下添加WebApiRequest.cs类,类的主要的两个方法如下: 做完这些。就可以在MainWindow里面愉快地调用WebAPI了。 ASP.NET Web API 实例,搜素材,soscw.com ASP.NET Web API 实例 标签:style class blog code http tar 原文地址:http://www.cnblogs.com/AlvinLiang/p/3808354.htmlASP.NET Web API 入门大杂烩
namespace Models
{
public class User
{
public long ID { get; set; }
public string UserName { get; set; }
public Platform Platform { get; set; }
public string Status { get; set; }
}
public enum Platform
{
Facebook,
Youtube
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Models;
using Newtonsoft.Json.Linq;
namespace VCoinWebApi.Controllers
{
public class UsersController : ApiController
{
List
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableSystemDiagnosticsTracing();
}
}
///
private void Button_AddUser_Click(object sender, RoutedEventArgs e)
{
User user = new User();
user.ID = 4;
user.Platform = Platform.Youtube;
user.Status = "User";
user.UserName = "Hello";
// myRequest.ContentType = @"application/json; charset=utf-8";//可以发送json字符串作为参数
// 在Web API端,对应的方法是 AddUser([FromBody]User user)
// 客户端发送一个对象的Json序列化字符串,在API端可以自动反序列化为具体对象
string userJsonString = JsonConvert.SerializeObject(user);
// myRequest.ContentType = "application/x-www-form-urlencoded";//只能发送&&&&&
// 在Web API端,对应的方法是Add([FromBody]long id, string userName,int platform,string status)
StringBuilder sb = new StringBuilder();
sb.Append(string.Format("id={0}", user.ID));
sb.Append(string.Format("&platform={0}", 0));
sb.Append(string.Format("&status={0}", user.Status));
sb.Append(string.Format("&userName={0}", user.UserName));
// 在API端的Add方法里面已经用ActionName注解属性强制指明了动作名称为AddUser
string path = "api/users/AddUser";
// myRequest.ContentType = @"application/json; charset=utf-8";//可以发送json字符串作为参数
// myRequest.ContentType = "application/x-www-form-urlencoded";//只能发送&&&&&
// 在WebApiRequest里面因为以及设置了前者,所以这里就发送Json字符串
string responseString = WebApiRequest.PostRequest(userJsonString, path);
WriteResult("添加User", responseString);
}
private void Button_UpdateUser_Click(object sender, RoutedEventArgs e)
{
//对应的方法是:UpdateUser([FromUri]int id,[FromBody]User user)
//1,是方法里面从Uri读取的参数
//user是序列化的Json字符串,需要从body读取
string path = "api/users/Update/1";
User user = new User();
user.ID = 1;
user.Platform = Platform.Youtube;
user.Status = "User";
user.UserName = "Hello";
string userJsonString = JsonConvert.SerializeObject(user);
string responseString = WebApiRequest.PostRequest(userJsonString, path);
WriteResult("更新User", responseString);
}
private void Button_GetAllUser_Click(object sender, RoutedEventArgs e)
{
string path = "api/users/GetAllUsers";
string responseString = WebApiRequest.GetRequest(path);
WriteResult("所有的User", responseString);
}
private void Button_GetProductByID_Click(object sender, RoutedEventArgs e)
{
int id = 0;
int.TryParse(TB_UserID.Text, out id);
string path = string.Format("api/users/GetUserById/{0}", id);
string responseString = WebApiRequest.GetRequest(path);
WriteResult("对应ID的User", responseString);
}
private void Button_GetUserByName_Click(object sender, RoutedEventArgs e)
{
string path = "api/users/GetUser";
string query = string.Format("userName={0}", TB_UserName.Text);
string responseString = WebApiRequest.GetRequest(path,query);
WriteResult("对应名字的User", responseString);
}
上一篇:python生成器与迭代器
下一篇:JavaSE