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.
You can access the Server
through the HttpContext
.
public void ProcessRequest(HttpContext context) {
context.Server.MapPath(...);
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With