2013年4月8日
摘要: 在项目中我们可能有时需要json也有可能需要xml,而且浏览器不同对返回的数据可以也会不同,也有可能根据设备的不同,有的设备操作xml方便,有的操作json方便,那我们的webapi接口返回的数据格式就需要都适应,但是一个借口怎么能同时支持xml与json呢?我们可以添加自己的 MediaTypeMapping打开自己的WebApiConfig文件,在 Register 方法中添加以下代码GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping( 阅读全文
posted @ 2013-04-08 17:15 认真的我 阅读(1217) 评论(1) 推荐(0) 编辑
摘要: webapi中的权限控制与mvc中的权限控制大致雷同,只是ActionFilterAttribute的命名空间不同在mvc中,如当前用户没有权限,直接在自己的 ActionFilterAttribute 中return就可以,但是在webapi中需要执行 actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);才可以我项目中的代码如下AuthenticationApiAttribute 1 using System; 2 using System.Net; 3 using System.Net. 阅读全文
posted @ 2013-04-08 17:09 认真的我 阅读(3847) 评论(0) 推荐(0) 编辑
摘要: 首先创建两个保存会话状态的类:SessionableControlHandler,SessionStateRouteHandler代码如下SessionableControlHandler 1 using System.Web.Http.WebHost; 2 using System.Web.Routing; 3 using System.Web.SessionState; 4 5 namespace iAssistantAPI.Authentication 6 { 7 public class SessionableControlHandler : HttpControllerHa... 阅读全文
posted @ 2013-04-08 17:04 认真的我 阅读(1631) 评论(2) 推荐(0) 编辑