From 9b8707b76a5fe097a92524fbcaa818219c1613de Mon Sep 17 00:00:00 2001 From: hishamco Date: Tue, 6 Sep 2022 15:40:22 +0300 Subject: [PATCH 1/2] Replace StatusCode() when its possible --- .../Controllers/DashboardController.cs | 8 ++++---- .../Controllers/NodeController.cs | 2 +- .../Controllers/PreviewController.cs | 2 +- .../OrchardCore.Contents/Controllers/ApiController.cs | 2 +- .../OrchardCore.Layers/Controllers/AdminController.cs | 10 +++++----- .../OrchardCore.Media/Controllers/AdminController.cs | 10 +++++----- .../OrchardCore.Setup/Controllers/SetupController.cs | 2 +- .../OrchardCore.Tenants/Controllers/ApiController.cs | 6 +++--- .../Extensions/ControllerExtensions.cs | 9 +++++++++ 9 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.AdminDashboard/Controllers/DashboardController.cs b/src/OrchardCore.Modules/OrchardCore.AdminDashboard/Controllers/DashboardController.cs index b8886ecb912..355986d4731 100644 --- a/src/OrchardCore.Modules/OrchardCore.AdminDashboard/Controllers/DashboardController.cs +++ b/src/OrchardCore.Modules/OrchardCore.AdminDashboard/Controllers/DashboardController.cs @@ -149,7 +149,7 @@ public async Task Update([FromForm] DashboardPartViewModel[] part { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminDashboard)) { - return StatusCode(401); + return Unauthorized(); } var contentItemIds = parts.Select(i => i.ContentItemId).ToArray(); @@ -161,7 +161,7 @@ public async Task Update([FromForm] DashboardPartViewModel[] part if (latestItems == null) { - return StatusCode(404); + return NotFound(); } foreach (var contentItem in latestItems) @@ -169,7 +169,7 @@ public async Task Update([FromForm] DashboardPartViewModel[] part var dashboardPart = contentItem.As(); if (dashboardPart == null) { - return StatusCode(403); + return Forbid(); } var partViewModel = parts.Where(m => m.ContentItemId == contentItem.ContentItemId).FirstOrDefault(); @@ -200,7 +200,7 @@ public async Task Update([FromForm] DashboardPartViewModel[] part if (Request.Headers != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest") { - return StatusCode(200); + return Ok(); } else { diff --git a/src/OrchardCore.Modules/OrchardCore.AdminMenu/Controllers/NodeController.cs b/src/OrchardCore.Modules/OrchardCore.AdminMenu/Controllers/NodeController.cs index 849a4023581..d25cac02341 100644 --- a/src/OrchardCore.Modules/OrchardCore.AdminMenu/Controllers/NodeController.cs +++ b/src/OrchardCore.Modules/OrchardCore.AdminMenu/Controllers/NodeController.cs @@ -265,7 +265,7 @@ public async Task Delete(string id, string treeNodeId) if (adminMenu.RemoveMenuItem(treeNode) == false) { - return new StatusCodeResult(500); + return this.InternalServerError(); } await _adminMenuService.SaveAsync(adminMenu); diff --git a/src/OrchardCore.Modules/OrchardCore.ContentPreview/Controllers/PreviewController.cs b/src/OrchardCore.Modules/OrchardCore.ContentPreview/Controllers/PreviewController.cs index ac1a900f7e1..8319f5dd4c4 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentPreview/Controllers/PreviewController.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentPreview/Controllers/PreviewController.cs @@ -92,7 +92,7 @@ public async Task Render() } } - return StatusCode(500, new { errors = errors }); + return this.InternalServerError(new { errors }); } var previewAspect = await _contentManager.PopulateAspectAsync(contentItem, new PreviewAspect()); diff --git a/src/OrchardCore.Modules/OrchardCore.Contents/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Contents/Controllers/ApiController.cs index 9858993faae..142a3076219 100644 --- a/src/OrchardCore.Modules/OrchardCore.Contents/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Contents/Controllers/ApiController.cs @@ -71,7 +71,7 @@ public async Task Delete(string contentItemId) if (contentItem == null) { - return StatusCode(204); + return NoContent(); } if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.DeleteContent, contentItem)) diff --git a/src/OrchardCore.Modules/OrchardCore.Layers/Controllers/AdminController.cs b/src/OrchardCore.Modules/OrchardCore.Layers/Controllers/AdminController.cs index a676a1b3568..e74aa08fdc5 100644 --- a/src/OrchardCore.Modules/OrchardCore.Layers/Controllers/AdminController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Layers/Controllers/AdminController.cs @@ -288,7 +288,7 @@ public async Task UpdatePosition(string contentItemId, double pos { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageLayers)) { - return StatusCode(401); + return Unauthorized(); } // Load the latest version first if any @@ -296,14 +296,14 @@ public async Task UpdatePosition(string contentItemId, double pos if (contentItem == null) { - return StatusCode(404); + return NotFound(); } var layerMetadata = contentItem.As(); if (layerMetadata == null) { - return StatusCode(403); + return Forbid(); } layerMetadata.Position = position; @@ -324,7 +324,7 @@ public async Task UpdatePosition(string contentItemId, double pos if (layerMetadata == null) { - return StatusCode(403); + return Forbid(); } layerMetadata.Position = position; @@ -341,7 +341,7 @@ public async Task UpdatePosition(string contentItemId, double pos if (Request.Headers != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest") { - return StatusCode(200); + return Ok(); } else { diff --git a/src/OrchardCore.Modules/OrchardCore.Media/Controllers/AdminController.cs b/src/OrchardCore.Modules/OrchardCore.Media/Controllers/AdminController.cs index 45fd6d46fb2..d52d5c651b7 100644 --- a/src/OrchardCore.Modules/OrchardCore.Media/Controllers/AdminController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Media/Controllers/AdminController.cs @@ -280,12 +280,12 @@ public async Task MoveMedia(string oldPath, string newPath) if (!_allowedFileExtensions.Contains(Path.GetExtension(newPath), StringComparer.OrdinalIgnoreCase)) { - return StatusCode(StatusCodes.Status400BadRequest, S["This file extension is not allowed: {0}", Path.GetExtension(newPath)]); + return BadRequest(S["This file extension is not allowed: {0}", Path.GetExtension(newPath)]); } if (await _mediaFileStore.GetFileInfoAsync(newPath) != null) { - return StatusCode(StatusCodes.Status400BadRequest, S["Cannot move media because a file already exists with the same name"]); + return BadRequest(S["Cannot move media because a file already exists with the same name"]); } await _mediaFileStore.MoveFileAsync(oldPath, newPath); @@ -385,7 +385,7 @@ public async Task> CreateFolder( if (_invalidFolderNameCharacters.Any(invalidChar => name.Contains(invalidChar))) { - return StatusCode(StatusCodes.Status400BadRequest, S["Cannot create folder because the folder name contains invalid characters"]); + return BadRequest(S["Cannot create folder because the folder name contains invalid characters"]); } var newPath = _mediaFileStore.Combine(path, name); @@ -399,13 +399,13 @@ public async Task> CreateFolder( var mediaFolder = await _mediaFileStore.GetDirectoryInfoAsync(newPath); if (mediaFolder != null) { - return StatusCode(StatusCodes.Status400BadRequest, S["Cannot create folder because a folder already exists with the same name"]); + return BadRequest(S["Cannot create folder because a folder already exists with the same name"]); } var existingFile = await _mediaFileStore.GetFileInfoAsync(newPath); if (existingFile != null) { - return StatusCode(StatusCodes.Status400BadRequest, S["Cannot create folder because a file already exists with the same name"]); + return BadRequest(S["Cannot create folder because a file already exists with the same name"]); } await _mediaFileStore.TryCreateDirectoryAsync(newPath); diff --git a/src/OrchardCore.Modules/OrchardCore.Setup/Controllers/SetupController.cs b/src/OrchardCore.Modules/OrchardCore.Setup/Controllers/SetupController.cs index 4be0f0f9451..35943664735 100644 --- a/src/OrchardCore.Modules/OrchardCore.Setup/Controllers/SetupController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Setup/Controllers/SetupController.cs @@ -61,7 +61,7 @@ public async Task Index(string token) if (!await ShouldProceedWithTokenAsync(token)) { - return StatusCode(404); + return NotFound(); } var model = new SetupViewModel diff --git a/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs index 7a9d7969c0c..248db8a5fbf 100644 --- a/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs @@ -113,7 +113,7 @@ public async Task Create(CreateApiViewModel model) var token = CreateSetupToken(settings); - return StatusCode(201, GetEncodedUrl(settings, token)); + return Created(GetEncodedUrl(settings, token)); } else { @@ -165,7 +165,7 @@ public async Task Setup(SetupApiViewModel model) if (shellSettings.State == TenantState.Running) { - return StatusCode(201); + return Created(GetEncodedUrl(shellSettings, null)); } if (shellSettings.State != TenantState.Uninitialized) @@ -280,7 +280,7 @@ public async Task Setup(SetupApiViewModel model) ModelState.AddModelError(error.Key, error.Value); } - return StatusCode(500, ModelState); + return this.InternalServerError(ModelState); } return Ok(executionId); diff --git a/src/OrchardCore/OrchardCore.Mvc.Core/Extensions/ControllerExtensions.cs b/src/OrchardCore/OrchardCore.Mvc.Core/Extensions/ControllerExtensions.cs index 2cdaa271127..1bde263b678 100644 --- a/src/OrchardCore/OrchardCore.Mvc.Core/Extensions/ControllerExtensions.cs +++ b/src/OrchardCore/OrchardCore.Mvc.Core/Extensions/ControllerExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Net; namespace Microsoft.AspNetCore.Mvc { @@ -27,6 +28,14 @@ public static ActionResult ChallengeOrForbid(this Controller controller) public static ActionResult ChallengeOrForbid(this Controller controller, params string[] authenticationSchemes) => controller.User?.Identity?.IsAuthenticated ?? false ? (ActionResult)controller.Forbid(authenticationSchemes) : controller.Challenge(authenticationSchemes); + /// + /// Creates that produces a response. + /// + /// The . + /// An optinal value to set on . + public static ActionResult InternalServerError(this Controller controller, object value = null) + => controller.StatusCode((int)HttpStatusCode.InternalServerError, value); + /// /// Creates a object that redirects to the specified local localUrl /// From 5bc09706aabd7381af2a5f0acaa2e1ae9aa745c4 Mon Sep 17 00:00:00 2001 From: hishamco Date: Tue, 6 Sep 2022 15:49:47 +0300 Subject: [PATCH 2/2] Fix Created() --- .../OrchardCore.Tenants/Controllers/ApiController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs b/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs index 248db8a5fbf..5add57e56ef 100644 --- a/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Tenants/Controllers/ApiController.cs @@ -113,7 +113,7 @@ public async Task Create(CreateApiViewModel model) var token = CreateSetupToken(settings); - return Created(GetEncodedUrl(settings, token)); + return Created(GetEncodedUrl(settings, token), null); } else { @@ -165,7 +165,7 @@ public async Task Setup(SetupApiViewModel model) if (shellSettings.State == TenantState.Running) { - return Created(GetEncodedUrl(shellSettings, null)); + return Created(GetEncodedUrl(shellSettings, null), null); } if (shellSettings.State != TenantState.Uninitialized)