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 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:
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)
Related
I've got a solution with 4 projects (Windows Service, Windows forms, Web and Shared code).
I would like all 4 apps to be able to write and read the database connection settings (server, db name, credentials) to an area where all apps can read it.
Currently it is being saved in the config file, but the Shared code app does not save it it's own config file, but in the app.config and web.config files respectively.
Is there a way that I can save it in a general space to be accessible by all?
Create another configuration file in the parent of all these apps in which you'll store the connection string.
Then customize the loading of the config file to take this file into account.
The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
</system.web>
<appSettings file="../externalSettings.config"/>
</configuration>
Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.
Reading connection string from external config file
You would also need to save the settings you want in that particular file
I always have doubt regarding how a path is formed whenever we run a windows app.
I have set a key like this in my app config
<add key="LogFilePath" value="..\Log\" />
When i run this from my local machine, it provides the path from where the windows app is run.
But when i run the same project from TFS, and when i try to create a file inside the Log folder , instead of the project mapped path it gives an entirely different path.
Can anyone tell me why this happens?
save the relative path in config and where you want to use it do it like this
string fullPath = Path.Combine(Application.StartupPath,configPath);
App Settings are very straight forward.
Add your properties to your App.Config app settings, e.g.
<appSettings>
<add key="LogFilePath" value="C:\Jaspreet_Files\LoadOrgInPortal.txt" />
</appSettings>
and read them, e.g.
var sqlConnectionString = System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"];
I guess the problem is .. in the value:
<add key="LogFilePath" value="..\Log\" />
This seems to be a relative path, try to get full path first before writing and see where it is writing and where it should.
When I built my .net site, I retrieved files paths using
Directory.GetFiles(#"D:\project\images");
But when I deployed it on internet server, I have to change the link of this folder, can you help me how can I do that?? can I use relative link in Directory.GetFiles() or how??
Put the path in your web.config. You shouldn't be hard coding paths anyway. What if it changes in the future?
In your settings add:
<appSettings>
<add key="myPath" value="D:\project\images"/>
</appSettings>
...and then call it from your application:
var myPath = WebConfigurationManager.AppSettings["myPath"];
If you really want to go nuts, look into web.config transformations so that when you publish, your release configuration will be transformed and applied for you!
in the web.config have something like:
<configuration>
<appSettings>
<add key="ImagesFolder" value="\Images" />
</appSettings>
</configuration>
then in your ASP.NET C# code behind you can use:
var ImagesFolder = ConfigurationManager.AppSettings["ImagesFolder"];
var files = Directory.GetFiles(Server.MapPath(ImagesFolder));
mind that you need to add a reference to the System.Configuration assembly or you won't be able to add the using statement and access the ConfigurationManager.
in this way there are no hard coded values and you can write the value you want for that appsetting by editing the web.config file in the deployed folder on the web server.
The correct way to do this is by using Server.MapPath
Which maps the virtual path in your web app to the physical path in the server.
I'm using a config file in my Library project in order to associate the interfaces with their own classes; I'm having troubles since my Application can't load anything from the config. Here is a sample from the config file,which is called app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.AuthenticateDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.AuthenticateDSImpl"/>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.CheckUserDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.CheckUserDSImpl"/>
<add key="Sic2Lib.it.carrefour.sic.profiler.datasource.def.ReadApplicationConfigDS" value="Sic2Lib.it.carrefour.sic.profiler.datasource.impl.ReadApplicationConfigDSImpl"/>
Which is held in the same directory of the DataSourceFactory Class. This class is supposed to take the setting through the command
NameValueCollection keys = ConfigurationManager.AppSettings;
So, I build the project with no errors and I get a file called myProject.dll.confing in the bin/Debug folder. But after all this I always get the keys variable empty...How come? What's wrong with what I've done so far?
Library projects do not have their own configuration - they use the configuration from the application that uses the library.
Put the configuration settings in the configuration file of the application project and you should be fine.
I have this C# project in Visual Studio (2010), and I'd like to refer to a file in my home directory in the <appSettings> section of the project's App.config file. That is, I use this syntax:
<appSettings>
<add key="Database" value="sqlite:///C:\Users\arvek\test.db3" />
</appSettings>
Is it possible to refer to my home directory (C:\Users\arvek) via a variable instead of hardcoding it directly? F.ex.: value="sqlite:///$HOME\test.db3".
The ConfigurationManager won't automatically expand anything in the app settings, since they are just free-form strings, but you can do so manually. Use the ExpandEnvironmentVariables method of Environment, which will expand variables of the form %VARIABLENAME% according to the current environment. So:
<appSettings>
<add key="Database" value="sqlite:///%APPDATA%\database\test.db3" />
</appSettings>
string path = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["Database"]);
The root path to your "home" directory is in the %USERPROFILE% variable, though %APPDATA% is the traditional place to put the kind of thing you're talking about. There is also %ALLUSERSPROFILE% for system-wide data (though in Windows 7 that actuallypoints to a special system-wide data folder, not the "Public" profile.)
I find it easier to just get the app's directory with this simple line of code
string appBasePath = AppDomain.CurrentDomain.BaseDirectory;
That's how I always refer to it.