Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.MapPath in ashx?

Tags:

c#

asp.net

I have an ashx file that serves PDF documents. Our environment is we develop locally, and the web app is moved to different environments: test and then production.

What is the best way to access a path on the server? How can I use Server.MapPath() in an .ashx handler.

like image 507
Sam Cromer Avatar asked Oct 04 '13 13:10

Sam Cromer


2 Answers

You can access the Server through the HttpContext.

public void ProcessRequest(HttpContext context) {

    context.Server.MapPath(...);
}
like image 161
Daniel A. White Avatar answered Oct 07 '22 13:10

Daniel A. White


I'd like to add another case to the scenario

Case one:

For example if you know the path to deploy and this isn't under the context of your application: from VS 2010 you can add a web.config transform depending on your build configuration

to keep it simple you can have a web.debug.config (let's assume development) and web.release.config (production) or you can set up your own build configuration as web.production.config if you want.

you can create an application setting for referencing the full path of the folder and do a transformation depending on which environment you are going to deploy

something like

<appSettings>
    <add key="folderPath" value="c:\dev" />
    // other keys here
</appSettings>

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="folderPath" value="c:\production" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

Case two: using the server mapPath as you mentioned

System.Web.HttpContext.Current.Server.MapPath() or context.Server.MapPath() 
like image 26
pedrommuller Avatar answered Oct 07 '22 14:10

pedrommuller