实现IHttpHandler接口,在web.config中配置处理器
- 创建asp.net web application(.net framework)空项目
- 添加空项 asp.net Handler,命名为queryInfo.cs
- 实现ProcessRequest方法
using System;
using System.Web;
using Newtonsoft.Json;
namespace WebApplication1
{
public class queryInfo : IHttpHandler
{
/// <summary>
/// You will need to configure this handler in the Web.config file of your
/// web and register it with IIS before being able to use it. For more information
/// see the following link: https://go.microsoft.com/?linkid=8101007
/// </summary>
#region IHttpHandler Members
public bool IsReusable
{
// Return false in case your Managed Handler cannot be reused for another request.
// Usually this would be false in case you have some state information preserved per request.
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//write your handler implementation here.
var info = new
{
name = "xiaowang",
age = 21,
sex = "男"
};
context.Response.ContentType = "application/json";
context.Response.Write(JsonConvert.SerializeObject(info));
context.Response.Flush();
}
#endregion
}
}
4、在web.config中增加如下节点
-
<?xml version="1.0"?> <configuration> <system.webServer> <handlers> <add name="queryInfo" path="/queryInfo" verb="GET" type="WebApplication1.queryInfo,WebApplication1"/> </handlers> </system.webServer> </configuration>
-
访问接口:
-
返回数据:
-
{"name":"xiaowang","age":21,"sex":"男"}