[英]Server.MapPath in ashx?
I have an ashx file that serves PDF documents. 我有一个提供PDF文档的ashx文件。 Our environment is we develop locally, and the web app is moved to different environments: test and then production.
我们的环境是我们在本地开发的,Web应用程序被移动到不同的环境:测试然后生产。
What is the best way to access a path on the server? 访问服务器上的路径的最佳方法是什么? How can I use
Server.MapPath()
in an .ashx handler. 如何在.ashx处理程序中使用
Server.MapPath()
。
You can access the Server
through the HttpContext
. 您可以通过
HttpContext
访问Server
。
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 例如,如果您知道要部署的路径,并且这不在您的应用程序的上下文中:从VS 2010,您可以添加web.config转换,具体取决于您的构建配置
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. 为了保持简单,您可以拥有web.debug.config(让我们假设开发)和web.release.config(生产),或者如果需要,您可以将自己的构建配置设置为web.production.config。
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 案例二:正如您所提到的那样使用服务器mapPath
System.Web.HttpContext.Current.Server.MapPath() or context.Server.MapPath()
If the PDF files are stored in a subdirectory of the one where your ashx is stored, you could use ~
as the root of your application: 如果PDF文件存储在存储ashx的子目录中,则可以使用
~
作为应用程序的根目录:
public void ProcessRequest (HttpContext context) {
// ...
context.Response.WriteFile("~/PDFs/onefile.pdf");
}
if your files are in a physical folder that is not a virtual directory, you could store the path in Web.Config (with a different configuration in dev, test and production) 如果您的文件位于不是虚拟目录的物理文件夹中,则可以将该路径存储在Web.Config中(在开发,测试和生产中使用不同的配置)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.