如何在asp.net中轻松自定义错误抛出

时间:2019-01-28 12:07:34

标签: asp.net-mvc asp.net-web-api breeze breeze-sharp

我的项目使用了微风并成功运行,但是我在SaveResult方法中遇到一个问题,我的要求是此方法自定义错误抛出,但不能正常工作。?

http://breeze.github.io/doc-cs/server-side-validation.html

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
     return _repository.SaveChanges(saveBundle);
}

protected override Dictionary<Type, List<Breeze.ContextProvider.EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<Breeze.ContextProvider.EntityInfo>> saveMap)
        {
             var ctx = new SlingBoardDataEntities();
            string activeUserOrg = OrganizationUserProfileService.CurrentUserProfile.OrganizationID;
            var MaxKeyword = ctx.SystemSettingsNew.FirstOrDefault(x => x.OrganizationID == activeUserOrg).MaxKeywordAllowed;
            int recordCount = 0;

            foreach (var entityData in saveMap)
            {
                var entityName = entityData.Key.Name;

                List<EntityError> authorizationErrors = new List<EntityError>();

                entityData.Value.ForEach(entity =>
                    {
                        ObjectPrivilegeActionTypes? activityType = null;

                        switch (entity.EntityState)
                        {
                            case Breeze.ContextProvider.EntityState.Added:
                                if (entityData.Key.GetType() == typeof(AuditLog))
                                {
                                    // always accept adding audit log

                                }
                                else
                                {
                                    //activityType = ObjectPrivilegeActionTypes.Create;
                                    switch (entityName)
                                    {
                                        case "FacebookKeywords":
                                            recordCount = ctx.FacebookKeywords.Count(x => x.OrganizationID == activeUserOrg);
                                            if (MaxKeyword >= recordCount)
                                            {
                                                activityType = ObjectPrivilegeActionTypes.Create;
                                            }
                                            else
                                            {
                                                authorizationErrors.Add(new EntityError()
                                                {
                                                    EntityTypeName = entity.GetType().FullName,
                                                    ErrorMessage = "your " + entityName + " keyword limitation is only " + MaxKeyword + activityType + "'",
                                                });
                                            }
                                            break;

                                        default:
                                            authorizationErrors.Add(new EntityError()
                                            {
                                                EntityTypeName = entity.GetType().FullName,
                                                ErrorMessage = "Unknown defaultResourceName with entityState " + entityName + activityType + "'",
                                            });
                                            break;
                                    } 
                                }
                                break;
                            case Breeze.ContextProvider.EntityState.Deleted:
                            case Breeze.ContextProvider.EntityState.Detached:
                                activityType = ObjectPrivilegeActionTypes.Delete;
                                break;
                            case Breeze.ContextProvider.EntityState.Modified:
                                activityType = ObjectPrivilegeActionTypes.Update;
                                break;
                            case Breeze.ContextProvider.EntityState.Unchanged:
                            default:
                                activityType = ObjectPrivilegeActionTypes.Access;
                                break;
                        }

                        if (activityType.HasValue && !AuthorizationService.AuthorizeObjectActivity(entityName, activityType.Value))
                        {
                            authorizationErrors.Add(new EntityError()
                            {
                                EntityTypeName = entity.GetType().FullName,
                                ErrorMessage = "Cannot execute the requestd activity '" + activityType + "'"
                                //KeyValues = new object[] { ((Order)oi.Entity).OrderID },
                                //PropertyName = "OrderID"
                            });
                        }
                    });

                if (authorizationErrors.Count > 0)
                {
                    throw new EntityErrorsException(authorizationErrors) { StatusCode = System.Net.HttpStatusCode.Unauthorized};
                }
            }
            return saveMap;
        }

public class EntityError
{
    public string ErrorName;
    public string EntityTypeName;
    public object[] KeyValues;
    public string PropertyName;
    public string ErrorMessage;
}

我的要求是>> ErrorMessage =“ your” + entityName +“关键字限制仅是” + MaxKeyword + activityType +“'” <<浏览器中显示此错误

0 个答案:

没有答案