add redirect helper on pagemodel; fix href bug in app-active tag helper
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using zero.Metadata;
|
||||
|
||||
@@ -9,17 +11,34 @@ public abstract class ZeroPageModel : PageModel
|
||||
/// <summary>
|
||||
/// Get access to the zero context for this request
|
||||
/// </summary>
|
||||
public IZeroContext ZeroContext => _zeroContext ?? (_zeroContext = HttpContext?.RequestServices?.GetService<IZeroContext>());
|
||||
public IZeroContext ZeroContext => _zeroContext ??= HttpContext?.RequestServices?.GetService<IZeroContext>();
|
||||
IZeroContext _zeroContext;
|
||||
|
||||
/// <summary>
|
||||
/// Get access to the localizer
|
||||
/// </summary>
|
||||
public ILocalizer Localizer => _localizer ?? (_localizer = HttpContext?.RequestServices?.GetService<ILocalizer>());
|
||||
public ILocalizer Localizer => _localizer ??= HttpContext?.RequestServices?.GetService<ILocalizer>();
|
||||
ILocalizer _localizer;
|
||||
|
||||
/// <summary>
|
||||
/// Set metadata for this page
|
||||
/// </summary>
|
||||
public MetadataOptions Metadata { get; protected set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Redirect to the current page with the same query string and an optional URL suffix
|
||||
/// </summary>
|
||||
public IActionResult RedirectToSelf(string suffix = null)
|
||||
{
|
||||
return Redirect(CurrentUrl(suffix));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get current URL as string with an optional URL suffix
|
||||
/// </summary>
|
||||
public string CurrentUrl(string suffix = null)
|
||||
{
|
||||
string rawUrl = Request.GetEncodedPathAndQuery();
|
||||
return rawUrl + suffix.Or(string.Empty);
|
||||
}
|
||||
}
|
||||
@@ -8,21 +8,21 @@ namespace zero.TagHelpers;
|
||||
[HtmlTargetElement(Attributes = "app-active")]
|
||||
public class ActiveTagHelper(IHttpContextAccessor contextAccessor) : TagHelper
|
||||
{
|
||||
[HtmlAttributeName("href")]
|
||||
public string Href { get; set; }
|
||||
|
||||
public override int Order { get; } = 100;
|
||||
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
string href = output.Attributes["href"]?.Value?.ToString();
|
||||
|
||||
output.Attributes.RemoveAll("app-active");
|
||||
|
||||
if (contextAccessor.HttpContext.IsPartOfUrl(Href))
|
||||
if (href == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (contextAccessor.HttpContext.IsPartOfUrl(href))
|
||||
{
|
||||
output.AddClass("is-active", HtmlEncoder.Default);
|
||||
}
|
||||
if (contextAccessor.HttpContext.IsUrl(Href))
|
||||
if (contextAccessor.HttpContext.IsUrl(href))
|
||||
{
|
||||
output.AddClass("is-active-exact", HtmlEncoder.Default);
|
||||
}
|
||||
@@ -33,15 +33,17 @@ public class ActiveTagHelper(IHttpContextAccessor contextAccessor) : TagHelper
|
||||
[HtmlTargetElement(Attributes = "app-active-exact")]
|
||||
public class ActiveExactTagHelper(IHttpContextAccessor contextAccessor) : TagHelper
|
||||
{
|
||||
[HtmlAttributeName("href")]
|
||||
public string Href { get; set; }
|
||||
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
string href = output.Attributes["href"]?.Value?.ToString();
|
||||
|
||||
output.Attributes.RemoveAll("app-active-exact");
|
||||
|
||||
if (contextAccessor.HttpContext.IsUrl(Href))
|
||||
if (href == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (contextAccessor.HttpContext.IsUrl(href))
|
||||
{
|
||||
output.AddClass("is-active-exact", HtmlEncoder.Default);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user