Static folder in solution with same name as Handler Path in web.config gives HTTP Error 500.0 - Internal Server Error

0

In my project I do have a static folder named StaticFiles where we usually keep some of the static files (pdf)

Currently users are accessing it via direct url, and since these all are static its getting served without going through the MVC pipeline. Now we are planning to come up with an extra security in accessing these files. We have added a handler in web.config like below

 <add name="ManagedPdfExtension"  path="StaticFiles/*.pdf" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Now this one is forcing the pdf files in StaticFile folder to go through MVC pipeline.

I have also added a method in MVC controller, for serving the file.

The issue is this one is not working as expected , its always throwing internal error when I try to access it via URL.

But this will work if I change the internal folder name to something else may be ABCD and access it like below.

[HttpGet]
        [Route("StaticFiles/{*any}")]
        public ActionResult CommonFiles(string attachmentName)
        {
            var filePathWithName = this.RouteData.Values["any"];
            var path = System.Web.Hosting.HostingEnvironment.MapPath("~/ABCD/"+ filePathWithName);
            path = path.Replace("//", "/").Replace("/","//");
            byte[] fileBytes = System.IO.File.ReadAllBytes(path);
            string fileName = "Test.pdf";

            var cd = new ContentDisposition
            {
                Inline = true,
                FileName = fileName
            };

            Response.Clear();
            Response.AddHeader(CoreConstants.ContentDisposition, cd.ToString());
            Response.AddHeader(CoreConstants.WindowTarget, CoreConstants.WindowTargetBlank);
            Response.BufferOutput = false;
            return File(fileBytes, "application/pdf");
        }

Any idea on, how can we go with StaticFiles folder instead of a new name ?? When ever the folder StaticNames added to the solution the url is going to internal server error.

Detailed Error Information:
Module     ManagedPipelineHandler
Notification       ExecuteRequestHandler
Handler    ManagedPdfExtension
Error Code     0x800703e9
Requested URL      http://localhost:62963/StaticFiles/test.pdf
 
c#
asp.net
asp.net-mvc
asp.net-mvc-5
web-config
asked on Stack Overflow Jan 21, 2021 by Arunprasanth K V • edited Jan 21, 2021 by Arunprasanth K V

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0