Placing the code for checking if a ModelState is invalid directly

This commit is contained in:
Elitsa Marinovska
2021-01-13 16:14:20 +01:00
parent ccfb915584
commit 3a51965358
3 changed files with 11 additions and 18 deletions

View File

@@ -1,22 +1,16 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Configuration;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Events;
using Umbraco.Core.Mapping;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Serialization;
using Umbraco.Core.Services;
using Umbraco.Core.Strings;
using Umbraco.Extensions;
using Umbraco.Web.Common.Exceptions;
using Umbraco.Web.Common.Filters;
using Umbraco.Web.Models.ContentEditing;
@@ -151,16 +145,6 @@ namespace Umbraco.Web.BackOffice.Controllers
}
}
protected virtual void HandleInvalidModelState(IErrorModel display)
{
//lastly, if it is not valid, add the model state to the outgoing object and throw a 403
if (!ModelState.IsValid)
{
display.Errors = ModelState.ToErrorDictionary();
throw HttpResponseException.CreateValidationErrorResponse(display);
}
}
/// <summary>
/// A helper method to attempt to get the instance from the request storage if it can be found there,
/// otherwise gets it from the callback specified

View File

@@ -570,7 +570,11 @@ namespace Umbraco.Web.BackOffice.Controllers
var display = _umbracoMapper.Map<MediaItemDisplay>(contentItem.PersistedContent);
//lastly, if it is not valid, add the model state to the outgoing object and throw a 403
HandleInvalidModelState(display);
if (!ModelState.IsValid)
{
display.Errors = ModelState.ToErrorDictionary();
return new ValidationErrorResult(display, StatusCodes.Status403Forbidden);
}
//put the correct msgs in
switch (contentItem.Action)

View File

@@ -7,6 +7,7 @@ using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@@ -266,7 +267,11 @@ namespace Umbraco.Web.BackOffice.Controllers
var display = _umbracoMapper.Map<MemberDisplay>(contentItem.PersistedContent);
//lastly, if it is not valid, add the model state to the outgoing object and throw a 403
HandleInvalidModelState(display);
if (!ModelState.IsValid)
{
display.Errors = ModelState.ToErrorDictionary();
return new ValidationErrorResult(display, StatusCodes.Status403Forbidden);
}
var localizedTextService = _localizedTextService;
//put the correct messages in