1.在asp项目中直接添加apiController,需要新增Global.asax文件。再增加一个webapiConfig,如果需要访问方式为"api/{controller}/{action}/{id}“ 修改路由:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using System.Web.SessionState; namespace AS.GroupOn.Web
{
public class Global : System.Web.HttpApplication
{ protected void Application_Start(object sender, EventArgs e)
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
} public override void Init()
{
this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;//用于打开session
base.Init();
} void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
System.Web.HttpContext.Current.SetSessionStateBehavior(
SessionStateBehavior.Required);
}
} public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{ config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;//设定返回方式 // config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
//config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
//config.Formatters.Remove(config.Formatters.JsonFormatter);
//config.Formatters.Remove(config.Formatters.XmlFormatter);
// 取消注释下面的代码行可对具有 IQueryable 或 IQueryable<T> 返回类型的操作启用查询支持。
// 若要避免处理意外查询或恶意查询,请使用 QueryableAttribute 上的验证设置来验证传入查询。
// 有关详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=279712。
//config.EnableQuerySupport(); // 若要在应用程序中禁用跟踪,请注释掉或删除以下代码行
// 有关详细信息,请参阅: http://www.asp.net/web-api
// config.EnableSystemDiagnosticsTracing();
}
}
}

2.定义HttpResponseMessage 返回类型。

using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

 private HttpResponseMessage getReturnMessage(int sta, string msg, HttpStatusCode httpStatus = HttpStatusCode.MethodNotAllowed)
{
return Request.CreateResponse(httpStatus, new{status=sta,info=msg});
} private HttpResponseMessage getReturnMessage(int sta,object obj, HttpStatusCode httpStatus = HttpStatusCode.OK)
{
return Request.CreateResponse(httpStatus, new { status = sta, obj});
}

最新文章

  1. 【转】Win7 64bit Oracle 11g 使用PL/SQL Developer 连接时提示“SQL*Net not properly installed”
  2. 移动开发发展方向-----Hybird混合开发3大方案
  3. MVC 4 异步编程简化了
  4. nginx端口被占用解决方案
  5. 关于cocostudio加载UI json CCUIHELPER未声明问题
  6. Struts2+Ajax实现检测用户名是否唯一
  7. CSS的标签类型
  8. 动手搭建第一个小程序音视频Demo
  9. HDU - 2154 线性dp
  10. vtop工具使用分析
  11. ZJOI2019一轮停课刷题记录
  12. Linux内核的冷热缓存
  13. day13_H5_CSS_1
  14. 【转】使用Eclipse,将鼠标放在相应方法或字段等元素上时,无法显示提示
  15. java开发师笔试面试每日12题(1)
  16. Python爬虫(三)——开封市58同城出租房决策树构建
  17. SNF快速开发平台MVC-EasyUI3.9之-Session过期处理和页面请求筛选
  18. php测试for/while/foreach循环速度对比
  19. Static变量与代码块
  20. Python - openpyxl 读写操作Excel

热门文章

  1. 20151208Study
  2. IOS常见错误之一连线错误
  3. phpredis中文文档 [转]
  4. python day2 列表的常用操作方法
  5. 开博了,hello world
  6. JavaScript-在当前显示区范围内实现点不到的小方块
  7. 使用KeepAlived搭建MySQL高可用环境
  8. Unity 4.x Asset Bundle 重名
  9. Python读取文本,输出指定中文(字符串)
  10. [转]jQuery实现清空table表格除首行外的所有数据