From abc277735777785e9dba1a97d383bd357a2f708f Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Mon, 11 Nov 2024 15:13:59 +0200 Subject: [PATCH] Adds URL solving service based on action and controller names --- .../Common/Services/IUrlService.cs | 12 ++++++ .../Common/Services/UrlService.cs | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/Lumina.Presentation.Web/Common/Services/IUrlService.cs create mode 100644 src/Lumina.Presentation.Web/Common/Services/UrlService.cs diff --git a/src/Lumina.Presentation.Web/Common/Services/IUrlService.cs b/src/Lumina.Presentation.Web/Common/Services/IUrlService.cs new file mode 100644 index 00000000..f28f6696 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Services/IUrlService.cs @@ -0,0 +1,12 @@ +namespace Lumina.Presentation.Web.Common.Services; + +public interface IUrlService +{ + /// + /// Generates an absolute URL for the specified controller action. + /// + /// The action name within the controller. + /// The controller name. + /// An absolute URL to the specified action. + string? GetAbsoluteUrl(string action, string controller); +} diff --git a/src/Lumina.Presentation.Web/Common/Services/UrlService.cs b/src/Lumina.Presentation.Web/Common/Services/UrlService.cs new file mode 100644 index 00000000..2f8ed3b5 --- /dev/null +++ b/src/Lumina.Presentation.Web/Common/Services/UrlService.cs @@ -0,0 +1,38 @@ +#region ========================================================================= USING ===================================================================================== +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +#endregion + +namespace Lumina.Presentation.Web.Common.Services; + +/// +/// Service for generating URLs from action and controller names. +/// +public class UrlService : IUrlService +{ + private readonly LinkGenerator _linkGenerator; + private readonly IHttpContextAccessor _httpContextAccessor; + + /// + /// Initializes a new instance of the class. + /// + /// The ASP.NET Core link generator service. + /// Provides access to the current HTTP context. + public UrlService(LinkGenerator linkGenerator, IHttpContextAccessor httpContextAccessor) + { + _linkGenerator = linkGenerator; + _httpContextAccessor = httpContextAccessor; + } + + /// + /// Generates an absolute URL for the specified controller action. + /// + /// The action name within the controller. + /// The controller name. + /// An absolute URL to the specified action. + public string? GetAbsoluteUrl(string action, string controller) + { + string? scheme = _httpContextAccessor.HttpContext?.Request.Scheme; + return _linkGenerator.GetUriByAction(_httpContextAccessor.HttpContext!, action: action, controller: controller, values: null, scheme: scheme); + } +}