Is there a way to read configuration files from multiple applications in the IIS worker process with relative path specification?

Asked 1 years ago, Updated 1 years ago, 46 views

I would like to read the configuration file in the relative path from the current folder on several web programs at asp.net in C#.

For example folder configuration below, I would like to be able to see aaa.xml of A app from the event of A app, and aaa.xml of B app from the event of B app.

IIS root folder <
AA Application ProProperties aaaa.xml
BB app ProProperties aaaa.xml

Is there a way to make it possible?

.NET is 4.6.2
The IIS operating server is Windows Server 2016.

c# asp.net

2022-09-29 22:09

1 Answers

For IIS+.NET Framework, use MapPath.

Specifies the relative path from the web root, not from the current folder.

string fullpath=Request.MapPath("~/config/aaa.config");

~/ will be replaced by the IIS root folder AA app in the top directory of the application.
If you have configured a virtual path in the IIS Management Console, you can view the real folder that corresponds to that virtual path.

Important: Security Considerations

IIS should be careful to avoid accidents where the configuration file is unexpectedly visible to the outside world.
I think the extension of the configuration file should be .config, not .xml.
IIS defaults to allow and deny HTTP access with extensions.
Even if you forget the per-folder deny setting, if the extension denies it, you can prevent the files from being viewed incorrectly from the outside.


2022-09-29 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.