简体   繁体   中英

Http Handler is working in iis express and not working in iis server

I am going to implement HttpHandler in order to allow file downloading from my site based on session values. If the session exist allow the user to download the file otherwise redirect to index page which is the login page for the site. My code is working perfect in iis express when I run my website in iis server the handler is not working.

For IIS express the web.config file has the following sections which I have added. The below configuration is working 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 which is not working is 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 am sure that in the section in the web.config file is not correct that is why it is not working. So I need suggestions to rectify my handlers section in web.config file. Any advice and help regarding this issue will be higly appreciated

Your IIS handler should be like this :

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

Two differences with your version :

  • only one file mask, you should register a handler for each file type
  • requireAccess="Script" with 'Script' having an upper-case 'S'

Hope this will help

To map a file-name extension in IIS 7.0 running in Classic mode

  1. Open IIS Manager.
  2. Expand the node for the Web server computer, expand Sites, and then expand Default Web Site.
  3. Select the node for your application. The Features View pane is displayed.
  4. In Features View, double-click Handler Mappings.
  5. On the Actions pane, click Add Script Map. The Add Script Map dialog box is displayed.
  6. In the Add Script Map dialog box, specify the following: o Request Path. The name or file-name extension to map. o 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). o Name. A descriptive name.
  7. Click OK to close the Add Script Map dialog box.
  8. Open the Web.config file for the application.
  9. Locate the httpHandlers element of the system.web section and add an entry for the file-name extension.

To map a file-name extension in IIS 7.0 running in Integrated mode

  1. Follow steps 1 through 3 of the previous procedure.
  2. On the Actions pane, click Add Managed Handler. The Add Managed Handler dialog box is displayed.
  3. In the Add Managed Handler dialog box, specify the following: o Request Path. The file name or file-name extension to map. o 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. o Name. A descriptive name.
  4. Click OK to close the Add Managed Handler dialog box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM