二开案例.自定义HttpHandler

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

二开案例.自定义HttpHandler

1、自定义HttpHandler,拿到请求的url和json进行处理,再返回json


using System.IO;
using System.Text;
using System.Web;
namespace Test2024.HttpHandler
{
    public class TestHandler : IHttpHandler
    {
        bool IHttpHandler.IsReusable
        {
            get
            {
                return true;
            }
        }
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            var request = context.Request;
            request.InputStream.Position = 0;
            StreamReader sr = new StreamReader(request.InputStream, Encoding.UTF8);
            var json = sr.ReadToEnd();
            //只要是/TestHandler/开头的url,都会到达这里。根据url和接收到的json,返回数据。例如 /k3cloud/TestHandler/test111
            var response = context.Response;
            response.Headers.Add("Content-Type", "application/json");
            var data = new
            {
                url = request.Url.ToString(),
                接收到的json = json
            };
            var responseJson = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data);
            response.Write(responseJson);
            response.End();
        }
    }
}
2、在Web.config中注册HttpHandler(Test24.PlugIns.BL为dll名称,Test2024.HttpHandler.TestHandler为自定义类的全名
<httpHandlers>
			<add name="TestHandler" path="TestHandler/*" verb="*" type="Test2024.HttpHandler.TestHandler,Test24.PlugIns.BL" />
</httpHandlers>

3、重启IIS,访问 localhost/k3cloud/TestHandler/test111

image.webp

4、发送json进行测试,在Postman中调用,或者打开 http://localhost/k3cloud ,按F12,在浏览器控制台输入以下代码:
$.ajax('/k3cloud/TestHandler/test111', {
                        method: 'POST',
                        contentType: "application/json" ,
                        dataType : 'json',
                        data: {test:"123456"},
                        success:function (result) {
                           console.log(result);
                        },
                        error:function (err) {
                            console.log('失败')
                        }
                    })

image.webp

5、公有云需要通过补丁包的形式修改Web.config,参考贴子 https://wenku.my7c.com/article/433641393274277632?productLineId=1&isKnowledge=2&lang=zh-CN


二开案例.自定义HttpHandler

1、自定义HttpHandler,拿到请求的url和json进行处理,再返回jsonusing System.IO;using System.Text;using System.Web;namespace Test2024...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息