ASP.NET MVC and IIS Express - Change executing directory - c#

Currently I am working on an ASP.NET MVC application that should use an existing framework. I am hosting this ASP.NET MVC app in IIS Express. Some classes of this framework assume that files are relative to the current directory. Right now the assemblies are executed in c:\users\MyName\appdata\local\temp\temporary asp.net files\root\3c076611\5261f232\assembly\dl3\d36edef7\e39ad394_8136d101.
Is it possible to change this directory?

var rootLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
now load your file as
var filePath = Path.Combine(rootLocation,"relative path to your assembly");
Edit:
if you cannot change framework, then you can change temp directory using web.config, add <compilation tempDirectory="C:\Project\Temp\">, but this will change only "c:\users\MyName\appdata\local\temp\temporary asp.net files\" part, rest of additional folders will still be there

Related

Is there any way to set a generic root path for content in ASP.NET Core?

I have faced problem with accessing static files from wwwroot folder in ASP.NET Core application in release mode. I keep my static files in wwwroot folder and access them with a relative path, for example to access wwwroot/images/test.png for src attribute of img tag I specify /images/test.png and the picture loads correctly in local IIS Express development mode.
Now I have created Website in IIS Manager (TestWebsite) and deployed my web application to it, and also I created Application pool in site (TestApplicationPool) so that it's hierarchy looks like this:
The problem I have faced is that after deploy my paths to static files has crashed and to access them I need to have /TestApplicationPool before any path (for the example above, now I need to specify /TestApplicationPool/images/test.png to access image). So my question is there any way we can configure ASP.NET application to always use current Application as content root path, so that we don't need to specify nothing in path except the path of static file relative to wwwroot folder?
I have tried to get application name from ContentRootPath variable of IWebHostEnvironment and insert it before the path to the static file and in this case everything works, but it is inconvenient to do this every time we need to load static file from wwwroot folder.
In MVC views or Razor pages you can use a tilde and slash (~/) prefix on the path to represent the web root (wwwroot by default) in most places in your cshtml views/pages.
For example:
<img src="~/hello.jpg" />
This should translate to the correct path even when deployed under a virtual directory in IIS (like your example).
Relevant documentation about web root can be found here, and this mentions the ~/
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-5.0&tabs=windows#web-root

C# get current directory without having to specify full path? [duplicate]

We have a web application written in ASP.NET 3.5. In it, we access a file in the code-behind. (Ordinary C# file access, done on the server during the page life-cycle, this has nothing to do with URLs or web browsers).
On our production systems, we specify the full path to the file in question in the web.config. We'd like to be able to include a local copy of the file in the project in version control, and then to use a relative path in the version-controlled web.config to point to it, so that a checked-out copy of the application could be run from within Visual Studio's debugger without having to do any configuration.
The problem is that when the web application is running in debug mode, its working directory is neither the project nor the solution directory. In a windows or console application, in a project's properties page I can set the working directory. But in a web application I cannot.
Any ideas on how I can manage to make this work?
To get the path of the root of the application:
//equivalent to Server.MapPath("/"); if at domain root, e.g Http://mysite.com/
string path = Server.MapPath("~");
This answer gives a rundown of a few different common Server.MapPath() uses that may also be of use to you.
In code behind: HttpContext.Current.Server.MapPath("~")
Use:
Server.MapPath("~");

How .Net names its temp folder inside C:\Windows\Microsoft.NET\Framework\v{version}\Temporary ASP.NET Files\root

I have some websites on my IIS machine. Lets say WebsiteA, WebsiteB and WebsiteC.
Inside .Net temp folder
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\{folderName}
each of the 3 website will have their own folder with some random name.
For example:
WebsiteA is using a folder named 5a3e1z1j. This folder will have the following path :
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\5a3e1z1j
WebsiteB is using a folder named b51o8881. This folder will have the following path :
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\b51o8881
WebsiteC is using a folder named ad4966a9. This folder will have the following path:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ad4966a9
How do I know which website is using which folder? So while the process of that website is still running, it won't allow me to delete the folder. It will only allow to delete the folder if I kill the process / stop the application pool or IIS.
Then I did some testing, what if I delete the folder (of course while there is no running process), what will happen to the site.
So apparently it will create a new folder with same exact random name.
Then I come up with a conclusion that this name is stored somewhere, so even though the folder is deleted. It will be recreated back with the same name.
My question is "How .Net create the name of these .Net temp folder so that for each website will have a consistent folder name? If it's stored somewhere after a successful build of the website, where can I find it?"
What I am trying to achieve is I want to create a tool that will do application pool restart if it's given input of website name. And also at the same time delete the .Net temp folder. But only the one associated to the input website.
If something is not clear, please let me know.
Thanks

IIS 7 how to load assembly from Bin folder

In .NET solutions I use custom classes for translations.
Main idea of translation framework
that files with translations are placed in folder near assembly.
All work fine when it calles from windows forms application.
But it does not work when I call it from web service...
I debug web service via Visual Studio 2010 and via bult-in debugger.
And I see that buit-in ASP.NET Developpment loades assemply from
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\
and there is no possibility to find my folder with translations...
So suggest please what to do in this case?
I tested under IIS7 it does not work also.
sample code how I load assembly:
if (languageSettings == null)
{
TraceIt(assembly.Location);
string strPath = Path.Combine(Path.GetDirectoryName(assembly.Location), "Language.config");
TraceIt(strPath);
languageSettings = new LanguageSettings(strPath);
if (!languageSettings.LoadSettings())
{
languageSettings.CurrentLanguage = DefaultLocale;
languageSettings.SaveSettings();
}
}
In web environment, its usual to setup a key in web.config with the absolute path to your language data folder, instead of rely on lookups in execution folder:
<appSettings>
<add key="Languages" value="D:\Data\Languages" />
</appSettings>
and, in code
stirng path = ConfigurationManager.AppSettings["Languages"]
if (!String.IsNullOrEmplty(path))
{
string file = Path.Combine(path, filename);
// and so on...
}
In a web application, your assemblies will be located in a bin folder. Assuming your config file is one level up, at the root of your application, you can get its path using Server.MapPath, like this (You'll need to reference System.Web).
string strPath = HttpContext.Current.Server.MapPath("~/Language.config");
You could try and get the location from the type. For example:
string strPath =
Path.Combine(
Path.GetDirectoryName(typeof(LanguageSettings).Assembly.Location),
"Language.config");
Take a look at the solution given by John Sibly :
How do I get the path of the assembly the code is in?
i think this is more what you are looking for ;) as it work in both cases (win and web)

How to select a folder relative to root in asp.net website?

I am trying to select all the files from a folder in my website and store them in a collection. The problem is that when I run the website it is not selecting the folder in my website:
This is the basic structure: [Root Folder] --> [FilesFolder]
Here is the code I am using:
DirectoryInfo dir = new DirectoryInfo("FilesFolder");
But it is showing this at runtime as the location of the folder:
C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\FileUploads
Is there a way to select the folder relative to the root of the website?
I am using C# with ASP.NET 3.5
You need to provide the full path to the web site when accessing it through the directory as in:
new DirectoryInfo("c:\inetpub\wwwroot\RootFolder\FilesFolder")
If you are trying to do this within an ASP.NET web site code, you can use Server.MapPath as in:
string path = Server.MapPath("~/FilesFolder");
Use:
Server.MapPath("~/FilesFolder");
More about it here: http://msdn.microsoft.com/en-us/library/ms178116.aspx

Categories

Resources