Sunil
Sunil

Reputation: 128

HttpHandler for prefix in url

I am trying to convert http call in aspx to https

Back Ground : i have a Aspx page that is in https site.on that page i have reference to script of google

Aspx page reference :

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>

i have created a HttpHandler for Prefix Http IHttpHandler Interface implementation :

public class HttpToHttpsHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.RawUrl.Contains("http:"))
            {
                string newUrl = context.Request.RawUrl.Replace("http", "https");
                context.Server.Transfer(newUrl);

            }
        }
        catch (Exception)
        {

            throw;
        }
    }
}

Web.Config file registration code :

  <httpHandlers>

    <add verb="*" path="http:*" type="HttpToHttpsHandler , App_Code"/>

  </httpHandlers>

But i am not getting the control in Http handler class.what could be the possible error.

Upvotes: 0

Views: 1413

Answers (3)

Patrick Hofman
Patrick Hofman

Reputation: 157068

I am wondering if your assembly is called "App_Code". In your type declaration you must enter the assembly name, not the folder name of the C# file.

Upvotes: 1

Suraj Singh
Suraj Singh

Reputation: 4069

You need to define your assembly name which contains the HttpToHandler class. The handler is defined as the class HttpToHttpsHandler in the your assembly which if is in the same project then it will be your application name. Go through this article

<httpHandlers>
      <add verb="*" path="*.aspx" 
         type="HttpToHttpsHandler , AssemblyName" />
    </httpHandlers> 
  </system.web>

if (!Request.IsLocal && !Request.IsSecureConnection)
{
    string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
    Response.Redirect(redirectUrl);
}

HttpRequest.IsSecureConnection Property determines whether the HTTP connection uses secure sockets ( HTTPS) or not .-MSDN

Upvotes: 0

Dalorzo
Dalorzo

Reputation: 20024

The path attribute as far as I know it is relative position and it only can take one of two values: the name/file-name or the extension/file-extension to map. Like

      <add verb="*" path="*.SampleFileExtension" 
         type="Example1 " />       

Or

      <add verb="*" path="demo.*" 
         type="Example1 " />     

I tried a combination of both these and it also worked, which says that anything that starts with test and for any extension will be handler by handler:

 <add  verb="*" path="test*.*"
                  name="HelloWorldHandler"
                  type="demo.HelloWorldHandler,App_Code" />

But please notice that it is a relative path, so it means it does not include the http or https values from the URL and therefore a Handler cannot be used to validate URLs.

Upvotes: 0

Related Questions