ASP.NET Web API 之一 入门篇
一、基于RESTful标准的Web Api
原文讲解:https://www.cnblogs.com/lori/p/3555737.html
微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码可读性强的,上手快的,如果要拿它和web服务相比,我会说,它的接口更标准,更清晰,没有混乱的方法名称,有的只有几种标准的请求,如get,post,put,delete等,它们分别对应的几个操作,下面讲一下:
GET:生到数据列表(默认),或者得到一条实体数据
POST:添加服务端添加一条记录,记录实体为Form对象
PUT:添加或修改服务端的一条记录,记录实体的Form对象,记录主键以GET方式进行传输
DELETE:删除 服务端的一条记录
自带的示例
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
} // GET api/values/5
public string Get(int id)
{
return "value";
} // POST api/values
public void Post([FromBody]string value)
{
} // PUT api/values/5
public void Put(int id, [FromBody]string value)
{ } // DELETE api/values/5
public void Delete(int id)
{
}
}
二、自定义的Web Api
自定义这里并没有多高明,说白了就是习惯了mvc的写法,不想用奇葩的restfull api
修改WebApiConfig即可实现
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务 // Web API 路由
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
); config.Filters.Add(new ValidataModelAttribute());
config.Filters.Add(new WebApiExceptionFilterAttribute());
// config.Filters.Add(new AuthFilterAttribute());//由于用java httpclient请求无法识别session,故而没使用了 config.Formatters.Remove(config.Formatters.XmlFormatter); }
}
WebApi没有session的,可在Global.asax开启session
public class WebApiApplication : System.Web.HttpApplication
{
//省略.... protected void Application_PostAuthorizeRequest()
{
if (isWebAPiRequest())
{
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}
}
private bool isWebAPiRequest()
{
return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/api");
}
}
三、使用模型验证
直接将步骤了,呵呵
1. 定义特性过滤:ValidataModelAttribute
响应结果:返回状态200,和错误结果提示,而不是400等其他状态,那样返回格式不一致,状态也不对
public class ValidataModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
string error = string.Empty;
foreach (var key in actionContext.ModelState.Keys)
{
var state = actionContext.ModelState[key];
if (state.Errors.Any())
{
error = state.Errors.First().ErrorMessage;
break;
}
}
var result = new ErrorResult(error);
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, result);
//actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
2.定义模型类
可参看:https://www.cnblogs.com/kexxxfeng/p/5602656.html
public class PageModel : BaseModel
{
[Required(ErrorMessage = "当前页不能为空")]
[Range(, ,ErrorMessage = "当前页必须大于0")]
public int Page { get; set; }
}
3.使用特性
这样就能自动验证了
[ValidataModel]
[HttpGet]
public IHttpActionResult GetPage([FromUri]PageModel model)
{
var dataGrid = xxx;
return JsonDataResult(dataGrid);
}
这里顺便讲下使用dynamic的问题,遇到 dynamic类型报错:“object”不包含“xxx”的定义
按网上的说法没法解决:https://www.cnblogs.com/similar/p/6716320.html
四、异常拦截
直接上代码
日志组件自己去搞定哈,WebApiConfig里面的配置注意配对
Application_Start下增加过滤: GlobalConfiguration.Configuration.Filters.Add(new WebApiExceptionFilterAttribute()); 这个不确定是否真的需要
public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute
{
//重写基类的异常处理方法
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//1.异常日志记录(正式项目里面一般是用log4net记录异常日志)
var msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "——" +
actionExecutedContext.Exception.GetType().ToString() + ":" + actionExecutedContext.Exception.Message + "——堆栈信息:" +
actionExecutedContext.Exception.StackTrace;
LogHelper.Fatal(msg); //2.返回调用方具体的异常信息
if (actionExecutedContext.Exception is NotImplementedException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
}
else if (actionExecutedContext.Exception is TimeoutException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);
}
//.....这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500
else
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
} base.OnException(actionExecutedContext);
}
}
五、TOKEN机制
这部分没做成功,这里只是记录下
其实,使用postman可以,客户端是别人做的,使用的是java httpclient,请求后,后台得不到当前用户session
public class AuthFilterAttribute : AuthorizeAttribute
{
public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
//取出区域的控制器controller,Action
string controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string action = actionContext.ActionDescriptor.ActionName;
if (controller.ToLower() == "account" && action.ToLower() == "login")
{
base.OnAuthorization(actionContext);
}
else
{
var content = actionContext.Request.Properties["MS_HttpContext"] as HttpContextBase;
var token = content.Request.QueryString["Token"];
if (!string.IsNullOrEmpty(token))
{
//URL路径
string filePath = HttpContext.Current.Request.FilePath;
//校验用户名密码是否匹配
if (ValidateTicket(token) && ValiddatePermission(token, controller, action, filePath))
{
base.IsAuthorized(actionContext);
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
//如果取不到身份验证信息,并且不允许匿名访问,则返回未验证401
else
{
var attributes = actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().OfType<AllowAnonymousAttribute>();
bool isAnonymous = attributes.Any(a => a is AllowAnonymousAttribute);
if (isAnonymous)
{
base.OnAuthorization(actionContext);
}
else
{
HandleUnauthorizedRequest(actionContext);
}
}
}
} private bool ValidateTicket(string encryptToken)
{
if (UserProvider.CurrentUser != null && UserProvider.CurrentUser.LoginToken == encryptToken)
{
return true;
} return false;
} public bool ValiddatePermission(string token, string controller, string action, string filePath)
{
//bool isPass = false;
//TODO 权限验证 return true;
}
}
另外,推荐几篇相关的文章
WebApi系列~StringContent参数需要添加MetaType对象
WebApi系列~通过HttpClient来调用Web Api接口
WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
六、使用request form
示例:https://www.cnblogs.com/FlowerThree/articles/10275476.html
var token = HttpContext.Current.Request.Headers["token"];
var uid = HttpContext.Current.Request.Form["uid"];
var aa = HttpContext.Current.Request.Form["list[aa]"];
ASP.NET Web API 之一 入门篇的更多相关文章
- Asp.Net Web API 2(入门)第一课
Asp.Net Web API 2(入门)第一课 前言 Http不仅仅服务于Web Pages.它也是一个创建展示服务和数据的API的强大平台.Http是简单的.灵活的.无处不在的.你能想象到几乎 ...
- JavaScript跨域调用、JSONP、CORS与ASP.NET Web API[共8篇]
[第1篇] 同源策略与JSONP 浏览器是访问Internet的工具,也是客户端应用的宿主,它为客户端应用提供一个寄宿和运行的环境.而这里所说的应用,基本是指在浏览器中执行的客户端JavaScript ...
- 01Getting Started---Getting Started with ASP.NET Web API 2入门WebApi2
HTTP 不只是为了生成 web 页面.它也是建立公开服务和数据的 Api 的强大平台.HTTP 是简单的. 灵活的和无处不在.你能想到的几乎任何平台有 HTTP 库,因此,HTTP 服务可以达到范围 ...
- 水果项目第3集-asp.net web api开发入门
app后台开发,可以用asp.net webservice技术. 也有一种重量级一点的叫WCF,也可以用来做app后台开发. 现在可以用asp.net web api来开发app后台. Asp.net ...
- ASP.NET Web API 2 入门
本文参考:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...
- [翻译]ASP.NET Web API 2入门
原文:Getting Started with ASP.NET Web API 2 Step 1:新建一个Empty的Web API Project. Step 2:添加一个Model: public ...
- ASP.NET Web API 2 入门(一)
前言 HTTP 不是只是为了服务的 web 页.这也是建设公开服务和数据的 Api 的强大平台.HTTP 是简单的. 灵活的和无处不在.你能想到的几乎任何平台有 HTTP 库,因此,HTTP 服务可以 ...
- ASP.NET Web API 2入门
1.全局配置 Web API 2之前是这样的配置的: protected void Application_Start() { //未实现特性路由 WebApiConfig.Register(Glob ...
- Web API 强势入门指南
Web API是一个比较宽泛的概念.这里我们提到Web API特指ASP.NET Web API. 这篇文章中我们主要介绍Web API的主要功能以及与其他同类型框架的对比,最后通过一些相对复杂的实例 ...
随机推荐
- odoo后台实现微信公众号验证
在微信公众号开发的其中一个步骤是微信服务器调用我们自己的网站验证身份,这一步微信服务器会传递过来4个参数,可是按照官方的写法,却无法验证通过,下面是官方的验证方法: import hashlib im ...
- RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your c
Error Msg: Traceback (most recent call last): File "<string>", line 1, in <module ...
- Neutron flat network 学习
flat network 是不带 tag 的网络,要求宿主机的物理网卡直接与 linux bridge 连接,这意味着: 每个 flat network 都会独占一个物理网卡. 在 ML2 配置中 ...
- JQ面向对象的放大镜
index.html <!DOCTYPE html><html> <head> <meta charset="utf-8" /> & ...
- 打开Player时出现时间格式的错误提示
安装完Player后如果更改了Windows的系统时间和日期显示格式,再次打开Player后会出现时间日期格式错误的提醒,需要按照要求更改Windows系统设置,才能正常运行Player. 此错误提示 ...
- vagrant之常用操作
基本操作: 查看版本: vagrant -v 初始化: vagrant init 启动虚拟机: vagrant up 关闭虚拟机: vagrant halt 重启虚拟机: vagrant reload ...
- php函数 array_combine
(PHP 5, PHP 7) array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 array_combine ( array $keys , array $ ...
- java学习之—实现一个简单的ArrayList
package thread1; /** * 实现一个简单的ArrayList * * @Title: uminton */ public class SimpleArrayList<T> ...
- [模板] 动态树/LCT
简介 LCT是一种数据结构, 可以维护树的动态加边, 删边, 维护链上信息(满足结合律), 单次操作时间复杂度 \(O(\log n)\).(不会证) 思想类似树链剖分, 因为splay可以换根, 用 ...
- linux 定时下载github最新代码
场景:网站的代码在github上托管,静态网站部署在服务器上,每次自己修改完本地代码后,提交到github上,需要自己去服务器上执行git pull 拉取最新代码, 为了解决这种操作,自己再服务器上 ...