javascript - 我可以在哪里存储 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>
解决方案
问题是我使用的路径:'~/App_Data/phones.xml'。相反,我创建了一个临时文件夹并使用了该路径
string path = (Request.Url.GetLeftPart(UriPartial.Authority) + "/Temp/phones.xml";
并使用剃须刀将其传递到我的 js 函数中。
推荐阅读
- c# - 如何将数据从 Form 传递到 Class 方法?
- python - 我的 Python 3 文件代码既没有附加也没有读取文件的权限
- angular - 使用 Angular Material 日期选择器自动格式化日期输入
- google-apps-script - 以编程方式查找您的脚本 ID
- amazon-web-services - 在另一个端口上运行 AWS SAM Local
- apache-spark - 使用 Window 函数将前一行与当前行相加
- python - Keras:获取 fit_generator (verbose=1) 和历史对象中的指标显示的不同值
- antlr - Antlr - 如何为输出生成精确的输入文件?(源到源的转换)
- internet-explorer - 使用 Oldie、PostCSS 和 Webpack 为 IE 生成单独的 CSS 包
- ios - AWS AppSync 使用错误的 Iam 角色