c# - 如何将错误消息从 C# 发送到 Ajax 以在客户端使用此错误消息的内容?

标签 c#

我目前正在努力将错误 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/

相关文章:

c# - 更改 PropertyGrid 中属性项的值

c# - ServiceController 类未导入 C#

c# - C# observer/observable with delegates 的 super 简单示例

c# - 类型 'System.String[]' 不支持比较运算符

c# - 如何在具有多个域的 ASP.NET MVC4 应用程序中允许 Windows 身份验证?

c# - Unity3d:通过GetComponent错误从另一个脚本访问类

c# - 是否有任何 C# 数学库可以进行插值/外推

c# - .Net ORM/业务对象框架性能

c# - ASP.NET 错误事件日志源属性的自定义值

c# - 在 ASP.NET MVC 4 中使用共享 View