{"id":150,"date":"2010-05-14T22:16:43","date_gmt":"2010-05-14T16:46:43","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=150"},"modified":"2010-05-14T22:16:43","modified_gmt":"2010-05-14T16:46:43","slug":"handlers-url-mapping-in-asp-net","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/handlers-url-mapping-in-asp-net\/","title":{"rendered":"Handlers in ASP.NET"},"content":{"rendered":"<p><strong>HTTP Handlers <\/strong>is\u00a0a new technique presented in ASP.NET that was not present in the &#8220;Classic ASP&#8221;.\u00a0<strong>HTTP Handlers<\/strong> are components that implement the <strong><em><span style=\"font-weight: normal;\"><span style=\"font-style: normal;\">System.<strong><em>Web<\/em><\/strong><\/span><\/span>.IHttpHandler<\/em><\/strong> interface. Unlike ASP.NET pages\u00a0Handlers dont have HTML-markup file, no events and other supporting. All they have is a code-file (written in any .NET-compatible language) that writes some data to the server HTTP response.<\/p>\n<p>ASP.NET handlers have &#8220;<strong>.ashx<\/strong>&#8221; file extension (unlike pages, that have &#8220;.aspx&#8221; file extension).<\/p>\n<p>Handlers are considered to be more lightweight object than pages. That&#8217;s why they are used to serve dynamically-generated images, on-the-fly generated PDF-files and similar content to the web-browser.<\/p>\n<p>One such scenario which may arise in any product is that if \u00a0I owe a site and I want to restrict the user from downloading a file if he\/she is not logged in. But if the user (not logged) is allowed to see the url of the downloading file then he can just copy the url and paste it in another tab and he can easily download the file from there, i.e. my protection mechanism failed. Now here comes the use of handlers.<\/p>\n<p>The handlers will restrict the user to even view \u00a0the url of the file to be downloaded unless he is logged in.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">How to create the Handlers in ASP.NET in Visual Studio 8.0.<\/span><\/strong><\/p>\n<p>First step is to create new project-<\/p>\n<p><strong>Step 1)<\/strong> Open Visual Studio an Goto &#8211; File Menu -&gt;New -&gt; Project.<\/p>\n<figure id=\"attachment_151\" aria-describedby=\"caption-attachment-151\" style=\"width: 494px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/AddProject.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-151\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/AddProject.jpg?resize=494%2C440&#038;ssl=1\" alt=\"Step 1\" width=\"494\" height=\"440\" \/><\/a><figcaption id=\"caption-attachment-151\" class=\"wp-caption-text\">Step 1<\/figcaption><\/figure>\n<p><strong><span style=\"font-weight: normal;\">Step 2<\/span><span style=\"font-weight: normal;\">)<\/span><span style=\"font-weight: normal;\"> Now new window will open then select \u00a0Web -&gt; ASP.NET Web Appliction. Then rename it and click ok.<\/span><\/strong><\/p>\n<figure id=\"attachment_163\" aria-describedby=\"caption-attachment-163\" style=\"width: 518px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step21.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-163  \" title=\"ASP.Net Handlers\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step21.jpg?resize=518%2C365&#038;ssl=1\" alt=\"Step 2\" width=\"518\" height=\"365\" \/><\/a><figcaption id=\"caption-attachment-163\" class=\"wp-caption-text\">Step 2<\/figcaption><\/figure>\n<p style=\"text-align: center;\"><strong><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/strong><\/p>\n<p><strong>Step 3)<\/strong> Now we have to add the Handler (Don&#8217;t call it the page, its all different thing) Follow the steps to include the handler in the current project.<\/p>\n<p>First Right Click on the Solution Explorer -&gt; Add -&gt; New Item -&gt;<\/p>\n<figure id=\"attachment_154\" aria-describedby=\"caption-attachment-154\" style=\"width: 259px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step-31.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-154\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step-31-259x300.jpg?resize=259%2C300&#038;ssl=1\" alt=\"\" width=\"259\" height=\"300\" \/><\/a><figcaption id=\"caption-attachment-154\" class=\"wp-caption-text\">Step 3<\/figcaption><\/figure>\n<p><strong>Step 4)<\/strong> Then new window will be displayed. Then select the Generic Handler and rename it then click on Add&#8211;&gt;<\/p>\n<p style=\"text-align: center;\">&nbsp;<\/p>\n<figure id=\"attachment_164\" aria-describedby=\"caption-attachment-164\" style=\"width: 505px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step-41.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-164   \" title=\"ASP.Net Handlers\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step-41.jpg?resize=505%2C305&#038;ssl=1\" alt=\"ASP.Net Handlers\" width=\"505\" height=\"305\" \/><\/a><figcaption id=\"caption-attachment-164\" class=\"wp-caption-text\">Step 4<\/figcaption><\/figure>\n<p><strong>Step 5) <\/strong> Now open the Default.asp page in Design view and design the form as required using Button Tool from toolbox.<\/p>\n<figure id=\"attachment_157\" aria-describedby=\"caption-attachment-157\" style=\"width: 425px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step-51.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-157\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Step-51.jpg?resize=425%2C409&#038;ssl=1\" alt=\"Step 5\" width=\"425\" height=\"409\" \/><\/a><figcaption id=\"caption-attachment-157\" class=\"wp-caption-text\">Step 5<\/figcaption><\/figure>\n<p><strong>Step 6)<\/strong> Then write the following code on the click event of both the buttons in Default.aspx.cs page (Just double click on the buttons and it will automatically generate the block in source page)<\/p>\n<p><span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px;\"> <\/span><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nprotected void btnWoutSecurity0_Click(object sender, EventArgs e)\n{\n     Response.Redirect(&quot;LinkSecurty.ashx?isSecure = 1&quot;);\n}\nprotected void btnWoutSecurity_Click(object sender, EventArgs e)\n{\n      Response.Redirect(&quot;LinkSecurty.ashx&quot;);\n}\n<\/pre>\n<div><strong>Step 7)<\/strong> Now open the Handler which we have created earlier i.e. LinkSecurity.aspx.cs \u00a0and write the following code&#8211;<\/div>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class LinkSecurity : IHttpHandler\n{\n     public void ProcessRequest(HttpContext context)\n     {\n        if (context.Request&#x5B;&quot;isSecure&quot;] == null)\n        {\n            context.Response.ContentType = &quot;text\/plain&quot;;\n            context.Response.Write(&quot;You are not Logged In.... Please Login to access this page...&quot;);\n        }else\n        {\n            context.Response.ContentType = &quot;image\/png&quot;;\n            context.Response.WriteFile(&quot;imageName.png&quot;)\n        }\n      }\n      public bool IsReusable\n      {   get{return false;}\n      }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HTTP Handlers is\u00a0a new technique presented in ASP.NET that was not present in the &#8220;Classic ASP&#8221;.\u00a0HTTP Handlers are components that implement the System.Web.IHttpHandler interface. Unlike ASP.NET pages\u00a0Handlers dont have HTML-markup file, no events and other supporting. All they have is a code-file (written in any .NET-compatible language) that writes some data to the server HTTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":323,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[21],"tags":[115],"class_list":["post-150","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","tag-http-handlers"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2395,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/asp-net-page-lifecycle\/","url_meta":{"origin":150,"position":0},"title":"ASP.NET Page Lifecycle","author":"Jitendra","date":"August 24, 2011","format":false,"excerpt":"Tutorial and sample source code on ASP.NET Page Lifecycle","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Control hierarchy after page_init() event in asp.net page life cycle","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Control-hierarchy-after-page_init-event-in-asp.net-page-life-cycle.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1527,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/ajax-based-multiselect-jquery-autocomplete-control-in-asp-net\/","url_meta":{"origin":150,"position":1},"title":"Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net","author":"Jitendra","date":"February 19, 2011","format":false,"excerpt":"Tutorial on creating Ajax Based Multiselect JQuery Autocomplete User Control in ASP.Net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Ajax-Based-Multiselect-JQuery-Autocomplete-Control-in-ASP.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":15,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/app_offline-htm-in-asp-net\/","url_meta":{"origin":150,"position":2},"title":"App_Offline.htm in ASP.NET","author":"Jitendra","date":"May 5, 2010","format":false,"excerpt":"If you place a file with the name App_Offline.htm in the root of a web application directory, ASP.NET 2.0 will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application. ASP.NET will also then respond to all requests for dynamic\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":125,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/cannot-access-a-closed-file-fileupload-in-asp-net\/","url_meta":{"origin":150,"position":3},"title":"cannot access a closed file &#8211; FileUpload in ASP.NET","author":"Jitendra","date":"May 13, 2010","format":false,"excerpt":"This was very\u00a0interesting\u00a0error, i got during development of file upload control in ASP.NET. On my local system, every thing was just fine. but when i deployed my application on development server, my control was able to upload only small size files. whenever i tried to upload large size file i\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":827,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/add-css-programatically-in-asp-net\/","url_meta":{"origin":150,"position":4},"title":"Add CSS Programatically in ASP.NET","author":"Jitendra","date":"September 3, 2010","format":false,"excerpt":"Tutorial on adding the CSS file dynamically in ASP.NET and C# \/ VB.Net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2299,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/tutorial-read-and-export-excel-file-in-asp-net-using-c\/","url_meta":{"origin":150,"position":5},"title":"Tutorial &#8211; Read and export excel file in ASP.Net  using C#","author":"Jitendra","date":"July 29, 2011","format":false,"excerpt":"Tutorial - Read and export excel file in ASP.Net using C#","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Read And Export Excel in ASP.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/07\/Read-And-Export-Excel-in-ASP.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}