当前位置: 首页 > news >正文

唐山建设信息网站电子商务网站的建设费用

唐山建设信息网站,电子商务网站的建设费用,亚星网站代理,宁波做网站的公司微软的 ASP.NET Web API 是一个轻量级的web框架#xff0c;可用来构建基于 http 无状态的rest服务#xff0c;异常是一种运行时错误#xff0c;异常处理是一种处理运行时错误的技术#xff0c;每一个开发者都应该知道如何处理 Web API 中的异常#xff0c;并且在 Action 中… 微软的 ASP.NET Web API 是一个轻量级的web框架可用来构建基于 http 无状态的rest服务异常是一种运行时错误异常处理是一种处理运行时错误的技术每一个开发者都应该知道如何处理 Web API 中的异常并且在 Action 中使用合适的 错误码 和 错误信息 进行包装。WebAPI 中的 HttpResponseException 你可以在 Action 中使用 HttpResponseException 来包装指定的 HttpCode 和 HttpMessage如下例子所示 public Employee GetEmployee(int id) {Employee emp  employeeRepository.Get(id);if (emp  null){var response  new HttpResponseMessage(HttpStatusCode.NotFound){Content  new StringContent(Employee doesnt exist, System.Text.Encoding.UTF8, text/plain),StatusCode  HttpStatusCode.NotFound}throw new HttpResponseException(response);}return emp; }如果你的 Action 返回的是 IHttpActionResult那么可将 GetEmployee() 方法修改如下 public IHttpActionResult GetEmployee(int id) {Employee emp  employeeRepository.Get(id);if (emp  null){var response  new HttpResponseMessage(HttpStatusCode.NotFound){Content  new StringContent(Employee doesnt exist, System.Text.Encoding.UTF8, text/plain),StatusCode  HttpStatusCode.NotFound}throw new HttpResponseException(response);}return Ok(emp); }从上面的代码可以看出错误码 和 错误消息 都赋给了 Response 对象然后包装到了 HttpResponseException 进行返回。WebAPI 中使用 HttpError 除了直接实例化 HttpResponseMessage 类还可以使用 Request.CreateErrorResponse() 快捷的创建 HttpResponseMessage 类如下代码所示 public IActionResult GetEmployee(int id) {Employee emp  employeeRepository.Get(id);if (emp  null){string message  Employee doesnt exist;throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, message));}return Ok(emp); }WebAPI 中使用 异常过滤器 异常过滤器是一种可以在 WebAPI 中捕获那些未得到处理的异常的过滤器要想创建异常过滤器你需要实现 IExceptionFilter 接口不过这种方式比较麻烦更快捷的方法是直接继承 ExceptionFilterAttribute 并重写里面的 OnException() 方法即可这是因为 ExceptionFilterAttribute 类本身就实现了 IExceptionFilter 接口如下代码所示[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited  true, AllowMultiple  true)]public abstract class ExceptionFilterAttribute : FilterAttribute, IExceptionFilter, IFilter{protected ExceptionFilterAttribute();public virtual void OnException(HttpActionExecutedContext actionExecutedContext);public virtual Task OnExceptionAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken);}下面的代码片段展示了如何通过重写 ExceptionFilterAttribute.OnException() 方法来创建一个自定义异常过滤器请注意下面的代码是如何捕获在 Action 中抛出的异常并将捕获到的异常转换为 HttpStatusResponse 实体然后塞入合适的 httpcode 和 httpmessage如下代码所示public class CustomExceptionFilter : ExceptionFilterAttribute{public override void OnException(HttpActionExecutedContext actionExecutedContext){HttpStatusCode status  HttpStatusCode.InternalServerError;String message  String.Empty;var exceptionType  actionExecutedContext.Exception.GetType();if (exceptionType  typeof(UnauthorizedAccessException)){message  Access to the Web API is not authorized.;status  HttpStatusCode.Unauthorized;}else if (exceptionType  typeof(DivideByZeroException)){message  Internal Server Error.;status  HttpStatusCode.InternalServerError;}else{message  Not found.;status  HttpStatusCode.NotFound;}actionExecutedContext.Response  new HttpResponseMessage(){Content  new StringContent(message, System.Text.Encoding.UTF8, text/plain),StatusCode  status};base.OnException(actionExecutedContext);}}接下来将自定义的异常过滤器添加到 HttpConfiguration 全局集合中如下代码所示public static void Register(HttpConfiguration config){config.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: DefaultApi,routeTemplate: api/{controller}/{id},defaults: new { id  RouteParameter.Optional });config.Formatters.Remove(config.Formatters.XmlFormatter);config.Filters.Add(new CustomExceptionFilter());}除了将自定义异常设置到全局上你还可以缩小粒度到 Controller 或者 Action 级别上下面的代码分别展示了如何将其控制在 Action 和 Controller 上。 [DatabaseExceptionFilter] public class EmployeesController : ApiController {//Some code }[CustomExceptionFilter]public IEnumerablestring Get(){throw new DivideByZeroException(); }ASP.NET Web API 提供了强大的 HttpResponseException 来包装异常信息默认情况下当 WebAPI 中抛出异常系统默认使用 Http StateCode 500 作为回应也即Internal Server Error. 场景就来了如果你会用 HttpResponseException 的话就可以改变这种系统默认行为自定义错误码和错误信息让结果更加清晰语义化。译文链接https://www.infoworld.com/article/2994111/how-to-handle-errors-in-aspnet-web-api.html
http://www.yutouwan.com/news/339627/

相关文章:

  • seo网站排名软件合肥做网站公司哪家好
  • 外贸网站怎样注册国外一个做同人动漫的网站
  • 宁波高端网站建设公司郑州正规的网站制作价钱
  • 交友深圳网站建设本地搭建php网站
  • 青浦网站设计制作简述制作网页时需要的环节
  • 建设银行官方网站 诚聘英才宁波自助建站模板
  • 代理平台登录镇江seo网站优化
  • 如何做网站截流阿里巴巴做网站教程
  • 金融行业建设网站百度企业网盘
  • ppt模板网站源码珠宝网站建设方案
  • 做网站的开发环境网站是什么软件
  • 郑州建网站费用网站开发客户哪里找
  • 简单 网站我爱水煮鱼的wordpress主题
  • 深圳坪山比亚迪最近的seo网站优化排名
  • 如何做网站网页费用做网站代理工作安全吗
  • 做网站的市场细分绿色电器公司网站psd模板
  • 网站怎么做的有创意泉州seo排名
  • 智慧团建网站登陆平台wordpress 标题字号
  • 南京有名的网站建设公司天河网站建设公司排名
  • 华东建设发展设计有限公司网站上海seo网站推广
  • 网站仿静态和静态的区别网站备案拍照背景幕布
  • 老鹰主机做的网站南通网站建设策划书
  • 站酷设计网站官网入口免费个人海报网站开发 旅游
  • 网站开发算互联网公司吗江苏百度推广代理商
  • 做网站要多少钱一个沧州有没有做网站的
  • 建e网官方网站求个网站没封的
  • 淄博高端网站建设乐达郑州做营销型网站公司
  • 公司网站建设详细方案从化公司网站建设
  • 带空间二字的韩国视频网站内蒙旅游
  • 17网站一起做网店池尾房地产中介网站