0
votes

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/*.

I have a web application that has a lot of javascript and ajax calls. 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.

I want the JavaScript client to be able to make RESTful calls, have the handler pick-up that call and process accordingly.

Here is what I have done so far:

  1. Implemented a class inheriting the IHTTPHandler Interface (this class is in the App_Code folder).
  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

I'd appreciate some help here. This is driving me nuts.

2

2 Answers

1
votes

In .Net Microsoft provide the rest architecture building api in WCF application that manage request and response over webHttpBinding protocol.

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

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)

<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 :

Creating Rest Services in asp.net with httphandler

-1
votes

You might want to look into the .Net Web API.