Where can I store an XML file for the browser to read using ASP.Net MCV

I would like to have a xml file on my website that holds temporary information from a database. When I use VSCode to do so, it works just fine; but when I use my actual ASP.Net Web Application, it does not. I assume it has to do with the file path that I am using, any help would be greatly appreciated!

ASP.Net
C#

string path = Server.MapPath("~/App_Data/phones.xml");
foreach (var pm in Model)
{
     // add each model to xml
}

JS
const MakePhoneOBJ = (xmlFile) => {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            xmlFunc(this);
        }
    };
    xmlhttp.open("GET", xmlFile, true);
    xmlhttp.send();
}

const xmlFunc = (xml) => {
    var xmlDoc = xml.responseXML;
    var root = xmlDoc.getElementsByTagName("Element");
    for (let i = 0; i < root.length; i++) {
        // Do something with the file contents
    }
}

HTML
<body onload="MakePhoneOBJ('@path');">
</body>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The issue was the path that I was using: ‘~/App_Data/phones.xml’. Instead, I created a temp folder and used that path

string path = (Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/phones.xml";

and passed it into my js function using razor.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x