diff --git a/src/SafeRouting.Common/Extensions.cs b/src/SafeRouting.Common/Extensions.cs
index 37ddd96..54355b9 100644
--- a/src/SafeRouting.Common/Extensions.cs
+++ b/src/SafeRouting.Common/Extensions.cs
@@ -27,6 +27,20 @@ public static RedirectToPageResult Redirect(this IPageRouteValues route, PageMod
public static RedirectToActionResult Redirect(this IControllerRouteValues route, PageModel page)
=> page.RedirectToAction(route.ActionName, route.ControllerName, route.RouteValues);
+ ///
+ /// Redirects () to the specified .
+ ///
+ /// The to redirect to.
+ /// The .
+ /// The .
+ public static ActionResult Redirect(this IRouteValues route, PageModel page)
+ => route switch
+ {
+ IControllerRouteValues controllerRoute => Redirect(controllerRoute, page),
+ IPageRouteValues pageRoute => Redirect(pageRoute, page),
+ _ => page.RedirectToRoute(route.RouteValues)
+ };
+
///
/// Redirects () to the specified .
///
@@ -45,6 +59,20 @@ public static RedirectToPageResult Redirect(this IPageRouteValues route, Control
public static RedirectToActionResult Redirect(this IControllerRouteValues route, ControllerBase controller)
=> controller.RedirectToAction(route.ActionName, route.ControllerName, route.RouteValues);
+ ///
+ /// Redirects () to the specified .
+ ///
+ /// The to redirect to.
+ /// The .
+ /// The .
+ public static ActionResult Redirect(this IRouteValues route, ControllerBase controller)
+ => route switch
+ {
+ IControllerRouteValues controllerRoute => Redirect(controllerRoute, controller),
+ IPageRouteValues pageRoute => Redirect(pageRoute, controller),
+ _ => controller.RedirectToRoute(route.RouteValues)
+ };
+
///
/// Generates a URL with a relative path for the specified .
///
@@ -70,4 +98,22 @@ public static RedirectToActionResult Redirect(this IControllerRouteValues route,
/// The value of host should be a trusted value. Relying on the value of the current request can allow untrusted input to influence the resulting URI unless the Host header has been validated. See the deployment documentation for instructions on how to properly validate the Host header in your deployment environment.
public static string? Url(this IControllerRouteValues route, IUrlHelper url, string? protocol = null, string? host = null, string? fragment = null)
=> url.Action(route.ActionName, route.ControllerName, route.RouteValues, protocol, host, fragment);
+
+ ///
+ /// Generates a URL with a relative path for the specified .
+ ///
+ /// The to generate the url for.
+ /// The .
+ /// The protocol for the URL, such as "http" or "https".
+ /// The host name for the URL.
+ /// The fragment for the URL.
+ /// The generated URL.
+ /// The value of host should be a trusted value. Relying on the value of the current request can allow untrusted input to influence the resulting URI unless the Host header has been validated. See the deployment documentation for instructions on how to properly validate the Host header in your deployment environment.
+ public static string? Url(this IRouteValues route, IUrlHelper url, string? protocol = null, string? host = null, string? fragment = null)
+ => route switch
+ {
+ IControllerRouteValues controllerRoute => Url(controllerRoute, url, protocol, host, fragment),
+ IPageRouteValues pageRoute => Url(pageRoute, url, protocol, host, fragment),
+ _ => url.RouteUrl(routeName: null, route.RouteValues, protocol, host, fragment)
+ };
}
diff --git a/src/SafeRouting.Common/TagHelpers.cs b/src/SafeRouting.Common/TagHelpers.cs
index a490c04..135b2ed 100644
--- a/src/SafeRouting.Common/TagHelpers.cs
+++ b/src/SafeRouting.Common/TagHelpers.cs
@@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
+using SafeRouting.Extensions;
namespace SafeRouting.TagHelpers;
@@ -47,22 +48,14 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
_ => null
};
- if (urlAttributeName is null)
+ if (urlAttributeName is null
+ || ForRoute is null
+ || ForRoute.Url(Url) is not string url)
{
return;
}
- var url = ForRoute switch
- {
- IControllerRouteValues controllerRoute => Extensions.RouteValueExtensions.Url(controllerRoute, Url),
- IPageRouteValues pageRoute => Extensions.RouteValueExtensions.Url(pageRoute, Url),
- _ => null
- };
-
- if (url is not null)
- {
- output.Attributes.SetAttribute(urlAttributeName, url);
- }
+ output.Attributes.SetAttribute(urlAttributeName, url);
}
private IUrlHelper Url => urlHelperFactory.GetUrlHelper(ViewContext);