简体   繁体   English

REST请求的自定义HTTP处理程序

[英]Custom HTTP Handler for REST Requests

I'm sure this is an easy fix - but after hours of googling this, I haven't been able to find the answer. 我敢肯定这是一个简单的解决方法-但是经过数小时的搜索后,我仍然找不到答案。

What I am trying to do is set up a custom handler that will handle all requests with a path of {domain}/services/*. 我要尝试做的是设置一个自定义处理程序,该处理程序将使用{domain} / services / *路径处理所有请求。

I have a web application that has a lot of javascript and ajax calls. 我有一个具有很多javascript和ajax调用的Web应用程序。 I want to implement a handler that will facilitate all the requests for other services (google maps, some custom services, etc.). 我想实现一个处理程序,以简化对其他服务(谷歌地图,某些自定义服务等)的所有请求。 This will allow me to leverage caching and simple configuration on the client. 这将使我能够在客户端上利用缓存和简单的配置。

My problem is with the implementation of the handler. 我的问题是处理程序的实现。 I cannot get IIS Express (or the built in VS 2010 web server for that matter) to trap the any requests with the path information above. 我无法让IIS Express(或内置VS 2010 Web服务器)来捕获具有上述路径信息的所有请求。

I want the JavaScript client to be able to make RESTful calls, have the handler pick-up that call and process accordingly. 我希望JavaScript客户端能够进行RESTful调用,让该调用的处理程序负责并进行相应的处理。

Here is what I have done so far: 到目前为止,这是我所做的:

  1. Implemented a class inheriting the IHTTPHandler Interface (this class is in the App_Code folder). 实现了一个继承IHTTPHandler接口的类(此类位于App_Code文件夹中)。
  2. Configured the Web.Config: system.webServer modules runAllManagedModulesForAllRequests="true" handlers add name="SeviceHandler" verb=" " path=" /services/*" type="MyWeb.UI.ServiceHandler, MyWeb.UI" resourceType ="Unspecified" handlers system.webServer 配置了Web.Config:system.webServer模块runAllManagedModulesForAllRequests =“ true”处理程序添加名称=“ SeviceHandler”动词=“ ” path =“ / services / *” type =“ MyWeb.UI.ServiceHandler,MyWeb.UI” resourceType =“未指定”处理程序system.webServer

I'd appreciate some help here. 在此感谢您的帮助。 This is driving me nuts. 这让我发疯。

In .Net Microsoft provide the rest architecture building api in WCF application that manage request and response over webHttpBinding protocol. 在.Net中,Microsoft提供了WCF应用程序中的其余体系结构构建api,这些API通过webHttpBinding协议管理请求和响应。

It may be more beneficial/efficient to implement a simple custom HttpHandler rather than invoking all of the plumbing and overhead associated with ASP.NET web services. 实现一个简单的自定义HttpHandler而不是调用与ASP.NET Web服务相关的所有管道和开销可能更有益/有效。 With a custom HttpHandler you can just send the parameter you need and return exactly the result you want to see without any of the supporting SOAP xml that would be created when using XML web services. 使用自定义的HttpHandler,您可以发送所需的参数,并完全返回要查看的结果,而无需使用XML Web服务时会创建的任何支持SOAP xml。

Bellow is the custom handler (We can also use .ashx file but there is no support of virtual path or virtual extension as custom handler provide) 波纹管是自定义处理程序(我们也可以使用.ashx文件,但是自定义处理程序提供的功能不支持虚拟路径或虚拟扩展名)

<system.web>
 <httpHandlers>
   <add verb="*" path="Services.fck" type="restHandler"/>
</httpHandlers> 

..... .....

if (Request.RequestType == "GET")
{

  //RawUrl=http://localhost/Services.fck/?Vivek/Gupta
  string RawUrl = Request.RawUrl;
  string splitter = "/?";
  string SubRawUrl = RawUrl.Substring(RawUrl.IndexOf(splitter) + splitter.Length);

  string[] Parameters = SubRawUrl.Split('/');
  if (Parameters.Length >= 2)
  {
   string name = Parameters[0];
   string surname = Parameters[1];
   string res = string.Format("Welcome {0} {1}", name, surname);
   JavaScriptSerializer jc = new JavaScriptSerializer();
   StringBuilder sb=new StringBuilder ();
   jc.Serialize(res, sb);
   Response.Write(sb.ToString());
   Response.ContentType = "application/json";
  }
}

For how to creating json, xml, plain text (GET, Post) based rest service with http handler and how to call them from jquery, asp.net, javascript See Below : 有关如何使用http处理程序创建json,xml,基于纯文本(GET,Post)的rest服务以及如何从jquery,asp.net,javascript调用它们的方法,请参见下文:

Creating Rest Services in asp.net with httphandler 使用httphandler在asp.net中创建Rest Services

您可能需要研究.Net Web API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对HTTP处理程序的同时请求不起作用 - Simultaneous requests to a HTTP Handler not working 自定义HTTP处理程序配置失败 - Custom HTTP handler configuration fail 使用自定义http处理程序的动态CSS问题 - Dynamic CSS using custom http handler issue 如何使用IIS下的HTTP处理程序处理长时间运行的请求? - How to process long running requests with an HTTP handler under IIS? 另一个ashx http处理程序之前的asp.net自定义http处理程序 - Asp.net custom http handler before another ashx http handler 更快地生成缩略图并在Handler中响应写入,或在Http模块中生成,然后让IIS处理其余的? - Faster to generate thumbnail and write to response in Handler or generate in Http module and let IIS handler the rest? 用于URL重写的自定义HTTP处理程序+会话和应用程序变量 - Custom HTTP handler for URL rewriting + session and application variable 自定义HTTP处理程序在IIS 7及更高版本和集成模式下不起作用 - Custom HTTP Handler Not Working in IIS 7 and above & Integrated Mode 使用jQuery加载自定义http处理程序ashx文件来显示png - Use jQuery load with a custom http handler ashx file to show a png 频繁的GET请求停止由HTTP处理程序实际处理/离开返回相同的值 - frequent GET requests stop being actually processed by HTTP handler / aways return same value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM