How to access a file from IIS web service hosted by ServiceHost - c#

In my IIS application I open a file located in the wwwroot directory that way:
File.ReadAllText("ConfigFile.json");
IISExpress tries to open the file in C:\Program Files (x86)\IIS Express\ConfigFile.json
I thought the wwwroot directory was the working directory but apparently it's not the case.
Log4net log files are written relatively to the working directory, and configuration manager files also. So I don't understand why opening a file with System.IO.File I have C:\Program Files (x86)\IIS Express as the working directory.
What's the best solution for that problem ? I suppose I don't have to touch the Current defined working directory.

Ok, the solution that works in a IIS WCF Service, hosted by a ServiceHost class is:
AppDomain.CurrentDomain.BaseDirectory + "/ConfigFile.json"
It gives the full absolute path that is valid in my IIS Express environment and in the deployed IIS environment.

You will need to inject IHostingEnvironment into your class to have access to the ApplicationBasePath property value
Suppose IHostingEnvironment type is env then you can use
File.ReadAllText(env.WebRootPath + "/ConfigFile.json");
like you have a function named Read then you can use
public void Read(IHostingEnvironment env)
{
File.ReadAllText(env.WebRootPath + "/ConfigFile.json");
}

Related

access outside project folder with Asp.net core application on linux

I'm having some issues trying to access a folder outside the application root folder with an asp.net application deployed on Linux.
The applcation is deployed at the moment (for testing purposes) in /home/pbl/projects/pbl-web/.
I want to access a folder of an external hard drive I've mounted on the system, and it's located in /mnt/ExtDist/Data.
I'm using a PhysicalFileProvider with the following path: /mnt/ExtDist/Data. This path is configured in app.settings.json file and retrieved through the IConfiguration configuration variable.
Here's a part of the code in the Startup.cs file :
public void ConfigureServices(IServiceCollection services)
{
...
var imagePath = configuration.GetSection("PhotoManagementSettings")["ImagesFolderPath"];
var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
this.imagePhysicalFileProvider = new PhysicalFileProvider(imagePath));
I've tried in different ways with no luck so far:
passing the absolute path
passing the relative path and combining with the rootPath variable (see the code above).
The PhysicalFileProvider is getting me the following error:
Unhandled exception. System.IO.DirectoryNotFoundException: /mnt/ExtDist/Data/
Testing the code in windows and giving it an absolute path like i.e "C:\Test" works fine.
So there's something weird in linux that is failing, but I cannot understand why. Any clues ?
Thanks in advance
Paolo

CSS folder not found on IIS .net core wwwroot

Somehow I decided to build my web API and client separately after several hours failed to combine .net core 2.2 with vue+vuetify.
I build the client using Vue CLI, with this config:
module.exports = {
publicPath: process.env.NODE_ENV === 'production' ? '/joblist/' : '/',
}
Then place the content of dist folder to wwwroot folder on .net core project, I use below code to enable UseStaticFiles:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
}
Everything looks perfect when I debug with APP URL set to http://localhost:49602/joblist. But somehow when I publish to my Server, which is myserver/joblist, the server can't serve EVERYTHING placed inside wwwroot/css folder, so the site looks ugly.
At first, I thought the problem is the wwwroot path or something like that, but I can browse to wwwroot/js folder perfectly, so I try to rename wwwroot/css folder to wwwroot/notcss, the site works perfectly this time.
Can anybody help me with this problem?
UPDATE:
The wwwroot/css and wwwroot/js is perfectly exist on the server.
I can open any file inside wwwroot/js folder but can not open any
file inside wwwroot/css folder.
For now, I use a workaround by creating a hard link named wwwroot/notcss (can be any) then replace any /css/* reference in index.html file to /notcss/*
UPDATE 2
Here is the error I received on the browser when browse on one of any file in CSS folder
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Detailed Error Information:
Module IIS Web Core
Notification MapRequestHandler
Handler StaticFile
Error Code 0x80070002
Requested URL http://localhost:80/joblist/css/app.14044b2f.css
Physical Path C:\inetpub\wwwroot\joblist\css\app.14044b2f.css
Logon Method Anonymous
Logon User Anonymous
There must be any some misscongured config, try to browse to C:\Windows\System32\inetsrv\config on your server, looks for a file named applicationHost.config open the file then search for css text. if you see some suspicious config just delete it, don't forget to backup it first.

UNC path is invalid when starting ASP.NET Core Application in IIS

I have a shared drive on my local network \hostname\shared\resource. I have mounted the drive programattically from within the Asp.net core web application in the startup.cs file.
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions{
FileProvider = new
PhysicalFileProvider(#"\\TS3410D618\NickFlicks\Movies"),
RequestPath = new PathString("/Movies"),
EnableDirectoryBrowsing = false
});
This works perfectly when I run it with IIS express(debugging), however, when I deploy it to my local IIS on the same machine the application fails to start with the error:
Application startup exception: System.ArgumentException: The directory name
\\TS3410D618\NickFlicks\MoviePosters\ is invalid.
This is a valid UNC path and can be reached from everywhere I have tried on my windows 10 machine. As I mentioned everything is on the same machine except for the shared drive.
As a side note I can mount a virtual drive from within IIS sucessfully, so IIS can access the fileshare without any issues.
I'm not sure what I am doing wrong here, any help would be greatly appreciated.
I could never get this to work because I couldn't find a way to create an Active Directory with Windows 10 or to modify the user permissions for the UNC file share for the user used by IIS. Basically, I just installed Ubuntu server with Nginx as the reverse proxy and everything worked fine once I set up the permissions properly, which was very straight forward.

application redirect to IIS root folder during upload excel file in asp.net

I am facing issue in my web application which is made in asp.net. I have host the application on IIS server.and I am going to upload excel file to port data in database from application.when i uploading from locally from server itself its working fine but i when i try to uploading using public ip or outside then my application redirect to IIS root folder can any one tell me whats is the issue.
this could be an permission issue on your web.config, file and folder permission or maybe a missing reference to correct folder.
Web.config
What is the authentication ? Do you allow Anonymous access. I understand that you can upload fine if you open the side from localhost from the server itself. Did you try to open the side with public ip from the server itself ?
Application Pool in IIS
Look for the "Identify" configuration and set this to "ApplicationPoolIdentify"
File Permission
Look for the folder and ensure that the "IIS_IUSERS" got write permissions
Code Try to sepecify the correct path using the server.MapPath()
Dim myPath As String = Server.MapPath("foldername") & "\" & "filename.csv"

Adding module for .exe file in IIS 6.0

I have problem with configuration for IIS 6.0.
I have a server which is used for download purpose, but for all files I would like to add custom header by a module:
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
String filePath = application.Request.Path;
String fileName = VirtualPathUtility.GetFileName(filePath);
application.Response.AddHeader("Content-Disposition", String.Concat("attachment; filename=\"", fileName, "\""));
}
My problem is that when I try to do that for .exe file it doesn't work.
I tried adding under mapping an extension to .exe -> c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll but that only forces to execute the .exe file on server.
I tried also with different execution permissions but setting anything other than Scripts and Executables resturns: HTTP Error 403.1 - Forbidden: Execute access is denied.
Any idea how to force that module to work with .exe files?
Does it allow you to download .exe file without your module?
On web site properties in IIS manager, Home Directory tab, make sure execute permissions is set to "scripts only".
On web site properties in IIS manager, HTTP Headers tab, be sure to define .exe as a valid MIME type.
Be sure to stop and restart the IIS Service (via Services)
Refer to http://blogs.msdn.com/b/david.wang/archive/2005/07/11/allow-file-downloads-on-iis-6.aspx

Categories

Resources