Http Handler works in iis express and does not work in iis server

I am going to implement an HttpHandler to allow file downloads from my site based on session values. If the session exists, the user can download the file, otherwise redirect to the index page, which is the login page for the site. My code works fine in iis express, when i run my site on iis server the handler doesn't work.

For IIS express, the web.config file has the following sections which I added. The following configuration works in iis express.

<system.web>

<httpHandlers>

  <add verb="*" path="*.pdf" type="QDMS.FileHandler" />
Same add tag for all the files to restrict downloading without session.

</httpHandlers>

</system.web>

      

The configurations for IIS servers not working are below.

<system.webServer>

<handlers>
  <add name="Files" path="*.pdf,*.doc,*.docx,*.rar,*.zip,*.ppt,*.pptx,*.jpg,*.png,*.bmp,*.gif,*.html,*.htm,*.pps" verb="*" type="QDMS.FileHandler" resourceType="Unspecified" requireAccess="script" />    
</handlers>

</system.webServer>

      

My file handler is below

using System;
using System.Web;
using System.Web.SessionState;
using QDMS.Old_App_Code;

namespace QDMS
{
public class FileHandler : IHttpHandler, IReadOnlySessionState
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
if (!CheckWetherTheRequestForFileExistOrNot(context)) return;
if (CheckUsersForFileDownloading(context))
context.Response.Redirect("~/index.aspx");
else
{
var rawURL = context.Request.RawUrl;
var dotIndex = rawURL.LastIndexOf(".", System.StringComparison.Ordinal);
var ext = rawURL.Substring(dotIndex);
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = MIMEEType.Get(ext);
context.Response.AddHeader("Content-Disposition", "attachment");
context.Response.WriteFile(rawURL);
context.Response.Flush();
}  
}
public bool CheckWetherTheRequestForFileExistOrNot(HttpContext context)
{
string url = context.Request.RawUrl.ToLower().Trim();
if (url.Contains(".pdf") || url.Contains(".xls") || url.Contains(".xlsx") || url.Contains(".jpg") ||
            url.Contains(".bmp") || url.Contains(".rar") || url.Contains(".doc") || url.Contains(".docx") ||
            url.Contains(".png") || url.Contains(".gif") || url.Contains(".pptx") || url.Contains(".zip") ||
            url.Contains(".ppt") || url.Contains(".pps") || url.Contains(".htm") || url.Contains(".html"))
return true;
else
return false;
}
public bool CheckUsersForFileDownloading(HttpContext context)
{
return (context.Session["FrontHiddenID"] == null) && (context.Session["HiddenID"] == null);
}
}
}

      

I'm pretty sure the section in the web.config file is wrong, so it doesn't work. So I need suggestions to fix the handlers section in the web.config file. Any advice and help on this issue would be much appreciated.

+3


source to share


2 answers


Your IIS handler should look like this:

<add name="Files" path="*.pdf" verb="*" type="QDMS.FileHandler" resourceType="Unspecified" requireAccess="Script" />

      

Two differences from your version:



  • only one file mask, you must register a handler for each file type
  • requireAccess="Script"

    with 'Script' uppercase 'S'

Hope this helps

+2


source


To display filename extension in IIS 7.0 running in classic mode

  • Open IIS Manager.
  • Expand the node for the web server machine, open Sites and then Default Website.
  • Select the node for your application. The Properties panel appears.
  • In Features view, double-click Handler Mappings.
  • In the Actions panel, click Add Script Map. The Add Script dialog box is displayed.
  • In the Add Script dialog box, specify the following: o Request path. The name or extension of the filename to display. o Executable file. The path of the .exe or .dll file that will process the request. In classic mode, specify the ASP.NET ISAPI extension (Aspnet_isapi.dll). o Name. A descriptive name.
  • Click OK to close the Add Script dialog box.
  • Open the Web.config file for the application.
  • Find the httpHandlers element in the system.web section and add an entry for the filename extension.


To match file name extension in IIS 7.0 running in integrated mode

  • Follow steps 1 through 3 of the previous procedure.
  • In the Actions panel, click Add Managed Handler. The Add Managed Handler dialog box opens.
  • In the Add Managed Handler dialog box, specify the following: o Request path. The filename or filename extension to match. o Type. The name of the type (class) of the managed handler. If a handler is defined in the App_Code folder of an ASP.NET application, its type name appears in the dropdown list. o Name. A descriptive name.
  • Click OK to close the Add Managed Handler dialog box.
+2


source







All Articles