【发布时间】:2021-06-09 09:53:03
【问题描述】:
在 localhost 创建 json 文件没有问题,但在生产中创建 json 文件得到错误代码 500 内部服务器错误。为什么会出现错误,我该如何解决?
代码 jquery
var url = '@Url.Action("CreateJsonFileContent", "xxx")';
$.ajax({
url: url,
type: "POST",
data: JSON.stringify(content),
dataType: "json",
contentType: "application/json; charset=utf-8"
}).done(function (data) {
console.log(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log("Status : " + textStatus);
})
代码控制器(部分)
[HttpPost]
public JsonResult CreateJsonFileContent(string PlantName, List<string> EmailName)
{
string json = JsonConvert.SerializeObject(contents);
string pathFile = System.Configuration.ConfigurationManager.AppSettings["pathJson"];
string jsonPath = Path.Combine(pathFile + "\\" + PlantName.Replace(" ", "") + ".json");
if (System.IO.File.Exists(jsonPath)) System.IO.File.Delete(jsonPath);
using (TextWriter tw = new StreamWriter(jsonPath))
{
tw.WriteLine(json);
}
return Json(message, JsonRequestBehavior.AllowGet);
}
【问题讨论】:
-
发生 500 时记录了什么异常?你调试了吗?
-
问题出在你的控制器(后端),而不是你的javascript(前端)。我猜这是一个文件访问问题。您需要启用日志记录以找出您的控制器抛出 500 错误的原因
标签: c# jquery asp.net asp.net-mvc asp.net-core