• Self referencing when returning chain of objects. This can be solved using a design pattern called theModel Factory.
  • We are returning all the fields from the domain model object and leaking sensitive information to the client, for example if you take a look on “Tutor” object you will notice that we are returning the “password” field which shouldn’t be leaked to API consumer. This can be solved using the Model Factory pattern.
  • Each resource returned in the response should be linked to a URI, this will simplify resources query for the client. This can be solved using the Model Factory pattern.
  • We should return HTTP status code when returning single resource, i.e if the resource was not found we should return 404 in response header, if it was found we should return 200 OK, etc…, this can be solved by returning HttpResponseMessage object.
  • Inside each method we are instantiating our repository, this operation is expensive as it includes opening connection to the database, we need to implement Dependency Injection pattern, this can be solved by using Ninject DI framework.
  • The format for JSON response objects are in Pascal Case i.e. “FirstName”, and most probably our API will be consumed in client using JavaScript, it is easier for JS developers to work with properties formatted in Camel Case “firstName”. this can be solved by configuring JSON formatters of the response.

返回对象时出现循环依赖,可以通过模型工厂模式解决。

我们返回了领域模型中所有的字段给客户端,然而有一些敏感信息不应该返回(例如:password字段),解决方案:模型工厂模式

每一个返回给客户端的资源都应该包含一个URI以便客户端查询,解决方案依旧是模型工厂模式。

对于返回单个资源,我们应当返回相应的状态码(例如:成功200,资源未找到404等),解决方案:HttpResponseMessage对象

在每个方法里我们都实例化了一个repository,这个对象包含了一些昂贵的操作(例如:数据库连接),解决方案:依赖注入模式

对于返回的Json对象格式是以“帕斯卡”风格的(例如“FirstName”),然而我们的Api有很大的可能被带有Javascript的客户端消费,对于JS开发者来说可能更适合“驼峰”风格(例如”firstName”)的数据。解决方案:配置Json格式。

asp.net web api 开发时应当注意的事项的更多相关文章

  1. Asp.Net Web API开发微信后台

    如果说用Asp.Net开发微信后台是非主流,那么Asp.Net Web API的微信后台绝对是不走寻常路. 需要说明的是,本人认为Asp.Net Web API在开发很多不同的请求方法的Restful ...

  2. 水果项目第3集-asp.net web api开发入门

    app后台开发,可以用asp.net webservice技术. 也有一种重量级一点的叫WCF,也可以用来做app后台开发. 现在可以用asp.net web api来开发app后台. Asp.net ...

  3. [目录]ASP.NET web api开发实战

    第一章:Restful web service v.s. RPC style web service 第二章:ASP.NET web api v.s. WCF v.s. ASP.NET web ser ...

  4. Asp.net Web Api开发Help Page配置和扩展

    为了方面APP开发人员,服务端的接口都应当提供详尽的API说明.但每次有修改,既要维护代码,又要维护文档,一旦开发进度紧张,很容易导致代码与文档不一致. Web API有一个Help Page插件,可 ...

  5. Asp.net Web Api开发Help Page 添加对数据模型生成注释的配置和扩展

    在使用webapi框架进行接口开发的时候,编写文档会需要与接口同步更新,如果采用手动式的更新的话效率会非常低.webapi框架下提供了一种自动生成文档的help Page页的功能. 但是原始版本的效果 ...

  6. Asp.net Web Api开发(第四篇)Help Page配置和扩展

    https://blog.csdn.net/sqqyq/article/details/52708613

  7. 基于.Net Framework 4.0 Web API开发(3):ASP.NET Web APIs 异常的统一处理Attribute 和统一写Log 的Attribute的实现

    概述:  ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是项目,总有异常发生,本节就来谈谈API的异常 ...

  8. ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API

    在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端通过CNBlogsAuthorizationServerProvider(Authorization ...

  9. ASP.NET Web API Controller 是怎么建成的

    先看ASP.NET Web API 讯息管线: 註:为了避免图片太大以至于超过版面,上图中的「HTTP 讯息处理程序」区块省略了 HttpRoutingDispatcher 处理路由分派的部分.「控制 ...

随机推荐

  1. css3 transition 实现图片放大缩小

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. echars3.0 柱状图y轴字体斜放

    xAxis: [ { type: 'category', axisLabel: { interval: 0, rotate: 45,//倾斜角度设置,是什么时针未测 margin: 2 //距离上部的 ...

  3. Asp_CRUD

    Asp_增删改查.逻辑流程 启动服务器. 地址为127.0.0.1 端口为随机分配 2607 然后在浏览器中输入http://localhost:2670/CRUD_main.ashx 浏览器像服务器 ...

  4. Asp.net 后台调用js方法(转)

    1. 用Response.Write方法 代码如下: Response.Write("<script type='text/javascript'>alert("XXX ...

  5. C# 每天温习一点(IEnumerable<TSource>)

    1, IEnumerable<TSource> 多数屌丝写成  IEnumerable<T>  无论TSource还是T都代表一个意思:要枚举的对象的类型 .IEnumerab ...

  6. python之平台独立的调试工具winpdb介绍

    Winpdb is a platform independent graphical GPL Python debugger with support for remote debugging ove ...

  7. linux中vsftpd登陆慢卡问题解决方法

    1.修改服务器上的/etc/resolv.conf令其内容只有类似 nameserver 192.81.133.229 nameserver 114.114.114.114 出现问题的机器的resol ...

  8. css 笔记——设置禁用中文输入法

    ime-mode ime-mode的语法解释如下: ime-mode : auto | active | inactive | disabled 取值: auto  : 默认值.不影响IME的状态.与 ...

  9. 实验一:点亮led

    一.先看原理图:

  10. grails导入excel

    grails导入excel,意思是说从excel表中读取多条数据,批量写入数据库. 有2种方案,1是使用grails的excel插件,2是调用java代码使用POI等第三方java控件. 今天比较累, ...