首页 > 解决方案 > 我可以在哪里存储 XML 文件供浏览器使用 ASP.Net MCV 读取

问题描述

我想在我的网站上有一个 xml 文件,其中包含来自数据库的临时信息。当我使用 VSCode 这样做时,它工作得很好;但是当我使用我实际的 ASP.Net Web 应用程序时,它没有。我认为它与我正在使用的文件路径有关,任何帮助将不胜感激!

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>

标签: javascriptc#asp.netxmlfile

解决方案


问题是我使用的路径:'~/App_Data/phones.xml'。相反,我创建了一个临时文件夹并使用了该路径

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

并使用剃须刀将其传递到我的 js 函数中。


推荐阅读