C# 创建json文件时产生错误500内部服务器错误
在localhost上创建json文件没有问题,但在产品上创建json文件得到错误代码500内部服务器错误。为什么会出现错误,如何修复 代码jqueryC# 创建json文件时产生错误500内部服务器错误,c#,jquery,asp.net,asp.net-mvc,asp.net-core,C#,Jquery,Asp.net,Asp.net Mvc,Asp.net Core,在localhost上创建json文件没有问题,但在产品上创建json文件得到错误代码500内部服务器错误。为什么会出现错误,如何修复 代码jquery var url = '@Url.Action("CreateJsonFileContent", "xxx")'; $.ajax({ url: url, type: "POST"
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(字符串PlantName,列表EmailName)
{
字符串json=JsonConvert.SerializeObject(内容);
字符串pathFile=System.Configuration.ConfigurationManager.AppSettings[“pathJson”];
字符串jsonPath=Path.Combine(路径文件+“\\”+PlantName.Replace(“,”)+“.json”);
如果(System.IO.File.Exists(jsonPath))System.IO.File.Delete(jsonPath);
使用(TextWriter tw=新StreamWriter(jsonPath))
{
tw.WriteLine(json);
}
返回Json(消息,JsonRequestBehavior.AllowGet);
}
与猜测一样,您的web应用程序很可能没有执行文件操作的权限,或者它正试图访问(读取和/或写入)一个不存在的位置
确定发生了什么的一种方法是在生产服务器上启用并激活日志记录。另一种(但可能非常不安全的方法)是向控制器方法添加一个try-catch语句,并将所有IO操作放在该try-catch语句中(参见下面的示例)
[HttpPost]
public JsonResult CreateJsonFileContent(字符串PlantName,列表EmailName)
{
字符串json=JsonConvert.SerializeObject(内容);
字符串pathFile=System.Configuration.ConfigurationManager.AppSettings[“pathJson”];
字符串jsonPath=Path.Combine(路径文件+“\\”+PlantName.Replace(“,”)+“.json”);
尝试
{
如果(System.IO.File.Exists(jsonPath))System.IO.File.Delete(jsonPath);
使用(TextWriter tw=新StreamWriter(jsonPath))
{
tw.WriteLine(json);
}
返回Json(消息,JsonRequestBehavior.AllowGet);
}
捕获(例外情况除外)
{
//此处记录错误
返回Json(新的
{
message=“创建文件时出错”,
详细信息=例如ToString()
},JsonRequestBehavior.AllowGet);
}
}
我称之为潜在的非常不安全的原因是,您正在向最终用户公开服务器错误的详细信息。在我看来,这是绝不允许的。也就是说,使用try-catch语句优雅地处理可能出现的异常,比如您正在遇到的异常,这是一个很好的实践
除此之外,如果您具有生产服务器上的相关访问权限,您可以尝试授予应用程序(以及正在执行该应用程序的用户)访问所述位置的权限。发生500时记录了什么异常?你调试过吗?问题出在你的控制器(后端),而不是你的javascript(前端)。我猜这是一个文件访问问题。您需要启用日志记录来找出控制器抛出500错误的原因
[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);
}
[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");
try
{
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);
}
catch (Exception ex)
{
// log error here
return Json(new
{
message = "An error occurred while creating the file",
details = ex.ToString()
}, JsonRequestBehavior.AllowGet);
}
}