二开案例.自定义HttpHandler原创
金蝶云社区-齐111
齐111
1人赞赏了该文章 39次浏览 未经作者许可,禁止转载编辑于2024年08月15日 09:48:47
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>

image.png

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.png

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


赞 1