0

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?

5
  • What is the error you are getting back? Use the F12 tools, turn on network tracing and see the exact message being returned. Without knowing the error we can only guess what the actual cause is. Commented Sep 11, 2016 at 14:22
  • Why not just use System.IO.File to save the file to disk? Why use XmlDocument if all you want to do is save the file to disk and not manipulate or parse the XML? Commented Sep 11, 2016 at 14:37
  • What error you are getting. ? Commented Sep 11, 2016 at 15:01
  • I am getting "Access to the path 'C:\inetpub\wwwroot\MyApp\MyFile.xml' is denied." 500 (Internal Server Error). @mason I am not educated on what libraries is best to use and when, sorry I only picked up this whole web dev 2 months ago, maybe you have some good resources? Commented Sep 11, 2016 at 15:40
  • Create a subdirectory of "MyApp" for your XML file(s) and give IIS_IUSERS write access to it. Use that subdirectory to write the files to. Commented Sep 11, 2016 at 15:54

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.