1

我的项目使用了微风并成功运行,但我在 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 + "'" << 这个错误显示在浏览器中

4

0 回答 0