简体   繁体   中英

How to save XML file on IIS 7 server using ASP.NET

So I have a problem in my ASP.NET MVC application, it doesn't want to save the xml file after I publish it. I have a form which I post to a controller using ajax, and then I use that data to create an xml file which i then want to save.

I use the following code to generate my xml file and save it:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(rawXml);
StreamWriter path = new StreamWriter(Server.MapPath("/"+ fileName + ".xml"));
xmlDoc.Save(path);

If I run my application in debug It writes the file to ~/MySolution/MyProject/MyFile , no problem.

So when I publish the app to the IIS 7 server on my computer and load the app through localhost/MyApp , I expect it to write the file to C:\\inetpub\\wwwroot\\MyApp\\MyFile but it doesn't.

I have enabled permissions to the folder inetpub and all the subsequent folders for NETWORK SERVICE . But the AJAX post keeps returning in Error and the file doesn't appear in the folder, so I assume it's not allowing to write the file to the specified path, or the path is incorrect, ether way I don't know how to check what's gone wrong.

How do I make the published app write the xml file to the C:\\inetpub\\wwwroot\\MyApp\\MyFile path?

First of all it's not recommended to write any files in the root folder as writing to root folder cause appdomain to recycle after certain number of writes (default is 15) causing session loss. See more details here .

I would suggest you to add a path of your server to web.config and then fetch it in your code.Use something like below in the appsettings section of web.config

<add key="filePath" value="C:\inetpub\wwwroot\MyApp" />

Regarding the permissions please add Users group to your folder and give that group full permission (read/write).

To further find out which specific user (as there are too many use cases) is used by w3wp you can use process monitor as explained below

Capture Process Monitor log while reproducing issue

  1. Filter on Access Denied
  2. No Access Denied, so filter on w3wp.exe
  3. Look for access to 401-3.htm
  4. Review entries before the 401-3.htm to determine what file was accessed last
  5. Check permissions based on successful QuerySecurityFile operation for last file accessed (in this case was asp.dll)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM