共用方式為


Coding Handlers

Handlers:

First we may want to know the different between two types of handlers:

Generic handler:

Generic Handler is a default handler which will have @webhandler directive and has .ashx extension this generic handler is not having UI but it provides response when ever any request made to this handler.

HTTP Handler:

HTTP Handler is a process which runs and continue to server request and give response based on the request handling code. This handler does not have UI and need to configured in the web.config against extensions. One of the great example of Http Handler is page handler of ASP.NET which serves .aspx pages request.

Difference between generic handler and http handler:

Following is a main differences between http handler and generic handler.

  1. Generic handler has a handler which can be accessed by url with .ashx extension while http handler is  required to be configured in web.config against extension in web.config.It does not have any extension
  2. Typical example of generic handler are creating thumbnails of images and for http handler page handler which serves .aspx extension request and give response.

 

Let us put it into practice :


ASP.NET HANDLER

*we are going to créate a simple proyect in asp.net web forms using VS2015 and then add a asp.net handler ítem which name is called IISHnadler1.cs.

 

IISHandler1.cs

 

 

using System;

using System.Web;

namespace test

{

public class IISHandler1 : IHttpHandler

{

#region IHttpHandler Members

public bool IsReusable

{

get { return true; }

}

public IISHandler1()

{

}

 

public void ProcessRequest(HttpContext context)

{

string RequestedPage = context.Request.Url.Segments[2].ToLower();

if (RequestedPage == "dan")

context.Response.Redirect("~/Default.aspx");

else if (RequestedPage == "dansau")

context.Response.Redirect("~/Default2.aspx");

else

{

HttpRequest Request = context.Request;

HttpResponse Response = context.Response;

Response.Write("<html>");

Response.Write("<body>");

Response.Write("<h1>Hello i m a custom HTTP handler.</h1>");

Response.Write("</body>");

Response.Write("</html>");

}

}

#endregion

}

}

What we specified there is the interface IHttpHandler  which is implemented those methods and the main one is ProcessRequest. On there as well as you can see i stated that when a request comes up take the second segment of the request, in order to understand it much better ..
Request.Url= www.microsoft.com/products/mypage.aspx
Request.Url.Segments[0]=www.microsoft.com
Request.Url.Segments[1]=products
Request.Url.Segments[2]=mypage.aspx

so, if request is "dan" or  "dansau" in the second segment it will be redirected to the mentiond page. if not, this will response with some text detailed above.

 

 

webconfig file

we should register handlers per request on web config file as follow:

IN INTEGRATED MODE:

<system.webServer>

<handlers>

<add verb="*" path="*sample" name="IISHandler1" type="test.IISHandler1"/>

</handlers>

IN CLASIC MODE:

We have to keep in mind that if we ran in classic mode on IIS7  this should be placed in <system.web>    <httpHandlers>  and also in >system.webserver> <Handlers> which must reference the aspnet_isapi.dll

Substitute the correct path of the aspnet_isapi.dll file. The .dll file is in the folder where the .NET Framework is installed. By default this is C:\WINDOWS\Microsoft.NET\Framework\versión.

something like this :

<configuration>
<system.web>
<httpHandlers><add verb="*" path="*.sample" type="IISHandler1"/></httpHandlers>
</system.web>
<system.webServer>
<handlers><add  verb="*" path="*.sample"name="IISHandler1"type="test.IISHandler1"modules="IsapiModule"/>scriptProcessor="%path%\aspnet_isapi.dll"</handlers>
</system.webServer>
</configuration>

 

Please check once hosted on IIS7 the ApplicationPool mode and then give it a try in order to test the results. https://yourdomain/sample/dan

 


 

 

Generic Handler:

 

webconfig file to map  a request  to the handler ashx file.

<system.web>
<urlMappings enabled="true">
<add url="~/Default.aspx" mappedUrl="~/Handler.ashx"/>
</urlMappings>
...

 

URL mappings. The  Web.config markup will automatically link one URL to another. When the Default.aspx page is requested, your Handler.ashx file will take over. This means you can map the default page in a directory to your handler.

 

<%@ WebHandler Language = "C#" Class="Handler" %>

using System;

using System.Web;

public class Handler : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "image/jpg";

context.Response.WriteFile("~/Flower1.jpg");

}

public bool IsReusable

{

get

{

return false;

}

}

}

 

 

as well as you can see any request which comes from  /Default.aspx  will point to the handler  generic file  and then if this is a .jpg  file this will return the right  picture in that format.

for more information please  go to https://msdn.microsoft.com/en-us/library/ms228090.aspx

 

 


 

Add Custom Hnadlers from IIS console:

 

Clasic mode [iis7,iis6]   handler

 

  1. In Features View from website , double-click Handler Mappings.
  2. On the Actions pane, click Add Script Map.
  3. The Add Script Map dialog box is displayed.
  4. In the Add Script Map dialog box, specify the following:
    • Request Path. The name or file-name extension to map.
    • Executable. The path of the .exe or .dll file that will handle the request. For Classic mode, specify the ASP.NET ISAPI extension (Aspnet_isapi.dll).
    • Name. A descriptive name.
  5. Click OK to close the Add Script Map dialog box.

 

 

Integrated mode: [iis7] handler

 

 

  1. On the Actions pane, click Add Managed Handler. The Add Managed Handler dialog box is displayed.
  2. In the Add Managed Handler dialog box, specify the following:
    • Request Path. The file name or file-name extension to map.
    • Type. The type (class) name of the managed handler. If the handler is defined in the App_Code folder of the ASP.NET application, its type name will appear in the drop-down list.
    • Name. A descriptive name.
  3. Click OK to close the Add Managed Handler dialog box.

 

for more information please go here : https://msdn.microsoft.com/en-us/library/bb515343(v=vs.100).aspx

Comments

  • Anonymous
    August 07, 2016
    beautiful Daniel