locked
Http Handler is working in iis express and not working in iis server RRS feed

  • Question

  • User1196193284 posted

    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 httphandler 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

    Wednesday, February 6, 2013 9:12 AM

Answers

  • User1196193284 posted

    I posted this question on stackoverflow and it is working now. 

    http://stackoverflow.com/a/14732457/1142645

    There is the answer in the link above

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 7, 2013 2:25 AM

All replies

  • User1196193284 posted

    I posted this question on stackoverflow and it is working now. 

    http://stackoverflow.com/a/14732457/1142645

    There is the answer in the link above

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 7, 2013 2:25 AM
  • User-1280676833 posted

    Congratulations! Im glad to hear that you have solved this issue by yourself. Welcome to post your question on here in the future programming.

    Regards

    Monday, February 18, 2013 4:30 AM