使用ActionResult:如果出现问题return new EmptyResult
,一切顺利throw an exception
return
。但是,由于并非所有代码路径都返回一个值,我需要在函数的底部添加某种public ActionResult GetPLUInfo()
{
try
{
//CSV FILE of PLUs
IEnumerable<PLURecord> listOfPLUs = _pluService.GetAll();
using (StreamWriter outfile = new StreamWriter(@"C:\Users\Martin\Desktop\chocListWRITE.csv", true)) //true: append text to a file with StreamWriter. The file is not erased, but just reopened and new text is added to the end.
{
foreach (PLURecord _plu in listOfPLUs)
{
string line = _plu.Serialize();
outfile.WriteLine(line);
Console.WriteLine(line);
}
}
HttpContext.ApplicationInstance.CompleteRequest();
return new EmptyResult();
}
catch (Exception ex)
{
Console.WriteLine("GetPLUInfo.");
Console.WriteLine(ex.Message);
}
//RETURN: How can i say here to return a http request error,
//Using the ActionResult
}
。优选地,http请求... 404可能。
我想要做的是从控制台应用广告运行此功能广告将PLU记录下载到CSV文件。这一切都很好,但是“返回”会引发错误....欢迎任何想法
{{1}}
答案 0 :(得分:0)
您可以将Response.StatusCode设置为有效的http错误,如:
Response.StatusCode = 500;
然后,您可以让web.config中的错误处理处理要做的事情,重定向到您自己的自定义操作或只返回页面。
答案 1 :(得分:0)
答案 2 :(得分:0)
你可以尝试这个
catch (Exception)
{
throw;
}