我目前正在努力将错误 C# 消息发送到 Ajax。在C#中我已经实现了管理器和 Controller ,它工作得很好。现在我的目的是向用户发送错误消息。用户应该在客户端看到错误信息消息,该错误消息弹出窗口有“确定”和“取消”。我已经实现了这个弹出功能。问题是如何从 C# Controller 发送此错误
我在C#管理器中添加了抛出异常,我可以看到客户端自动弹出错误,而无需处理此错误。
C# Controller
[HttpPatch]
[CatchException]
public IHttpActionResult ChangePositioningPlan(ChangePositioningPlan changeCommand)
{
changePositioingPlan.Process(changeCommand);
return Ok("true");
}
C# 管理器
public void Process(ChangePositioningPlan command)
{
if (!command.Ids.Any())
{
throw new Exception("Please select Position Plan Template.");
}
int i = 0;
foreach (var positioningPlanId in command.Ids)
{
var positioningPlanToChange = positioningPlanRepository.FindBy(positioningPlanId);
positioningPlanToChange.ShippingSerieses.Clear();
string[] shippingIds = Array.ConvertAll(command.ShippingIds[i].Split(','), p => p.Trim());
var list = new List<ShippingSeries>();
foreach (var shippingId in shippingIds)
{
var existingShippingId = shippingSeriesRepository.FilterBy(sid => shippingId == sid.ShippingId).FirstOrDefault();
if (existingShippingId == null)
{
throw new ArgumentException($"Shipping Id={shippingId} is not found.");
}
list.Add(existingShippingId);
var checkExistingDateWithOtherPlanIds = positioningPlanRepository.FilterBy(d =>
positioningPlanToChange.DeliveryDate == d.DeliveryDate).ToList();
//var test = checkExistingDateWithOtherPlanId.ShippingSerieses.Contains(existingShippingId);
foreach (var existingPlanId in checkExistingDateWithOtherPlanIds)
{
var checkPlanId = existingPlanId.ShippingSerieses.Contains(existingShippingId);
if (checkPlanId)
{
throw new ArgumentException($"Shipping Id Has already assigned to PlanId:" +
$"Shipping ID -> Plan ID " +
$"{existingShippingId.ShippingId} -> {existingPlanId.PlanId}, {positioningPlanToChange.PlanId}");
}
}
}
最佳答案
好的,根据您的反馈,让我们再进行一次(更干净的)尝试。尝试这样的事情:
public class ChangePositioningPlan
{
public bool SomePropertyMissing { get; set; }
public bool SomeOtherPropertyMissing { get; set; }
}
//This is your Manager class
public class SomeObject : ActionResult
{
private ChangePositioningPlan _command;
public SomeObject(ChangePositioningPlan command)
{
_command = command;
_command.SomePropertyMissing = true; //Error
}
public override void ExecuteResult(ControllerContext context)
{
if (_command.SomePropertyMissing)
{
context.HttpContext.AddError(new ArgumentException("GetFeedback: Something was not found!"));
return; //Whether you want to return or continue execution?
}
if (_command.SomeOtherPropertyMissing)
{
context.HttpContext.AddError(new Exception("ShowInfo: Something else was not found!"));
return; //Whether you want to return or continue execution?
}
}
}
public class HomeController : Controller
{
public ActionResult Index(ChangePositioningPlan command)
{
return new SomeObject(command);
}
}
然后您可以查看响应消息是否包含 GetFeedback 或 ShowInfo,将其过滤掉并向用户显示您的错误。
可能有改进这一点并删除“ShowInfo”和“GetFeedback”的潜力,但我想不出什么办法。也许尝试问另一个专门解决这个问题的问题。
关于c# - 如何将错误消息从 C# 发送到 Ajax 以在客户端使用此错误消息的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589322/