title: Why does this ASP.NET Web Handler not receive the POST request?
body: <p>I was looking at an ASP.NET app example that someone else wrote, which basically consists of an HTML file sending an AJAX POST request to a Web Handler - which is then supposed to process the request and send back a message to say that it was received successfully.</p>
  <p>However, this example code seems to return a &quot;500 status (Internal Server Error)&quot;, and i really can't seem to figure out why. Would someone kindly review the code and tell me why this is happening? I would <em>really</em> appreciate it.</p>
  <p>I wanted analyse the code to use it in a search engine project, which is supposed to send a user input string from a front-end html form to the Web Handler, this string would then be processed and sent to a crawler which would crawl sites from a database and return the sites which have the highest volume of the key-word.</p>
  <p>Here is the HTML file with the AJAX POST request:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
     &lt;meta charset=&quot;UTF-8&quot;&gt;
     &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
     &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
     &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js&quot;&gt;&lt;/script&gt;
     &lt;title&gt;Search Engine&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;table&gt;
         &lt;tr&gt;
             &lt;td&gt;&lt;label id=&quot;From&quot; /&gt;From : &lt;/td&gt;
             &lt;td&gt;&lt;input id=&quot;TxtFrom&quot;&gt;&lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
             &lt;td&gt;&lt;label id=&quot;To&quot; /&gt;To : &lt;/td&gt;
             &lt;td&gt;&lt;input id=&quot;TxtTo&quot; required=&quot;required&quot;&gt;&lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
             &lt;td&gt;&lt;label id=&quot;To&quot; /&gt;Body : &lt;/td&gt;
             &lt;td&gt;
                 &lt;textarea id=&quot;TxtBody&quot; required=&quot;required&quot;
                           rows=&quot;10&quot; cols=&quot;30&quot;&gt;&lt;/textarea&gt;
             &lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
             &lt;td /&gt;
             &lt;td style=&quot;align-content:flex-end&quot;&gt;
                 &lt;button onclick=&quot;loadJsonData();&quot;&gt;Send Data to the Server&lt;/button&gt;
             &lt;/td&gt;
         &lt;/tr&gt;
     &lt;/table&gt;
     &lt;script&gt;
         function loadJsonData() {
             var postdata = JSON.stringify(
                 {
                     &quot;From&quot;: document.getElementById(&quot;TxtFrom&quot;).value,
                     &quot;To&quot;: document.getElementById(&quot;TxtTo&quot;).value,
                     &quot;Body&quot;: document.getElementById(&quot;TxtBody&quot;).value
                 });
             try {
                 $.ajax({
                     type: &quot;POST&quot;,
                     url: &quot;MailHandler.ashx&quot;,
                     cache: false,
                     data: postdata,
                     dataType: &quot;json&quot;,
                     success: getSuccess,
                     error: getFail
                 });
             } catch (e) {
                 alert(e);
             }
             function getSuccess(data, textStatus, jqXHR) {
                 alert(data.Response);
             };
             function getFail(jqXHR, textStatus, errorThrown) {
                 alert(jqXHR.status);
             };
         };
     &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Here is the Web Handler (MailHandler.ashx):</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net.Mail;
  using System.Web;
  using System.Web.UI;
  
  namespace CodeProject
  {
      public class MailHandler : IHttpHandler
      {
          public void ProcessRequest(HttpContext context)
          {
              string jsonString = String.Empty;
              HttpContext.Current.Request.InputStream.Position = 0;
              using (System.IO.StreamReader inputStream = new System.IO.StreamReader(HttpContext.Current.Request.InputStream))
              {
                  jsonString = inputStream.ReadToEnd();
                  System.Web.Script.Serialization.JavaScriptSerializer jSerialize = new System.Web.Script.Serialization.JavaScriptSerializer();
                  var email = jSerialize.Deserialize&lt;Mail&gt;(jsonString);
  
                  if (email != null)
                  {
                      string froma = email.From;
                      string to = email.To;
                      string body = email.Body;
  
                      context.Response.Write(jSerialize.Serialize(
                           new
                           {
                               Response = &quot;Message Has been sent succesfully&quot;
                           }));
                  }
              }
          }
  
          public bool IsReusable
          {
              get
              {
                  return false;
              }
          }
      }
  }
  </code></pre>
  <p>Web Config:</p>
  <pre><code>&lt;configuration&gt;
    &lt;system.web&gt;
      &lt;customErrors mode=&quot;Off&quot;/&gt;
      &lt;compilation debug=&quot;true&quot; targetFramework=&quot;4.5&quot;&gt;
        &lt;assemblies /&gt;
      &lt;/compilation&gt;
      &lt;httpRuntime targetFramework=&quot;4.5&quot; /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;httpErrors errorMode=&quot;Detailed&quot; /&gt;
      &lt;handlers&gt;
        &lt;remove name=&quot;ExtensionlessUrlHandler-Integrated-4.0&quot; /&gt;
        &lt;remove name=&quot;OPTIONSVerbHandler&quot; /&gt;
        &lt;remove name=&quot;TRACEVerbHandler&quot; /&gt;
        &lt;add name=&quot;ExtensionlessUrlHandler-Integrated-4.0&quot; path=&quot;*.&quot; verb=&quot;*&quot; type=&quot;System.Web.Handlers.TransferRequestHandler&quot; preCondition=&quot;integratedMode,runtimeVersionv4.0&quot; /&gt;
      &lt;/handlers&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>