Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow Requests to App_Data

I want to allow users to request files located in App_Data Folder. This is the error:

Error Summary

HTTP Error 404.8 - Not Found

The request filtering module is configured to deny a path in the URL that contains a hiddenSegment section.

like image 350
levi Avatar asked May 09 '12 11:05

levi


2 Answers

Its not possible to access App_Data folder directly as it is used as data-storage for web application, for security reasons of the stored data you can access database from it only using connectionstring.

web.config

<connectionStrings>
    <add name="AddressBookConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

check this http://www.codeproject.com/Articles/31557/A-Beginner-s-Guide-to-ASP-NET-Application-Folders#h

UPDATE
Programatically we can access any file in web application and write it to response:

public class FileAccessHandler:IHttpHandler
{
    public FileAccessHandler()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        String FileName = Path.GetFileName(context.Request.PhysicalPath);
        String AssetName = HttpContext.Current.Request.MapPath(Path.Combine(HttpContext.Current.Request.ApplicationPath, "App_Data/" + FileName));

        if (File.Exists(AssetName))
        {
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(File.ReadAllBytes(AssetName));
            context.Response.End();
        }
    }
}


Download: App_Data access sample

like image 189
Nitin S Avatar answered Sep 26 '22 19:09

Nitin S


This is not recommended as app data is meant for application files but this can be done by adding the following line to the config

  <system.webServer>
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="app_data" />
        </hiddenSegments>
      </requestFiltering>
    </security>
  </system.webServer>
like image 41
Lord Darth Vader Avatar answered Sep 22 '22 19:09

Lord Darth Vader