Skip to content

Instantly share code, notes, and snippets.

@ProRedCat
Created May 7, 2025 07:43
Show Gist options
  • Select an option

  • Save ProRedCat/bd579e7fe8471dfa184bd0cd1b8b144e to your computer and use it in GitHub Desktop.

Select an option

Save ProRedCat/bd579e7fe8471dfa184bd0cd1b8b144e to your computer and use it in GitHub Desktop.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
namespace R2.Common.Extensions;
/// <summary>
/// Static class for safe url helper extension methods.
/// </summary>
public static class SafeUrlHelperExtensions
{
private const string DefaultErrorMessage = "URL generation failed. The generated URL cannot be null.";
/// <summary>
/// Generates a URL with a path for an action method, throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action();
if (url is not null)
{
return url;
}
throw new InvalidOperationException(DefaultErrorMessage);
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> name, throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(action);
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}'.");
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> name and route <paramref name="values"/>, throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <param name="values">An object that contains route values.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action, object? values)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(action, values);
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}'.");
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> and <paramref name="controller"/> names, throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <param name="controller">The name of the controller.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action, string? controller)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(action, controller);
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
var controllerName = controller ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}' on controller '{controllerName}'.");
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> name, <paramref name="controller"/> name, and route <paramref name="values"/>,
/// throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <param name="controller">The name of the controller.</param>
/// <param name="values">An object that contains route values.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action, string? controller, object? values)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(action, controller, values);
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
var controllerName = controller ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}' on controller '{controllerName}'.");
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> name, <paramref name="controller"/> name, route <paramref name="values"/>, and
/// <paramref name="protocol"/> to use, throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <param name="controller">The name of the controller.</param>
/// <param name="values">An object that contains route values.</param>
/// <param name="protocol">The protocol for the URL, such as "http" or "https".</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action, string? controller, object? values, string? protocol)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(action, controller, values, protocol);
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
var controllerName = controller ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}' on controller '{controllerName}'.");
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> name, <paramref name="controller"/> name, route <paramref name="values"/>,
/// <paramref name="protocol"/> to use, and <paramref name="host"/> name, throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <param name="controller">The name of the controller.</param>
/// <param name="values">An object that contains route values.</param>
/// <param name="protocol">The protocol for the URL, such as "http" or "https".</param>
/// <param name="host">The host name for the URL.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action, string? controller, object? values, string? protocol, string? host)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(action, controller, values, protocol, host);
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
var controllerName = controller ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}' on controller '{controllerName}'.");
}
/// <summary>
/// Generates a URL with a path for an action method, which contains the specified
/// <paramref name="action"/> name, <paramref name="controller"/> name, route <paramref name="values"/>,
/// <paramref name="protocol"/> to use, <paramref name="host"/> name, and <paramref name="fragment"/>,
/// throwing an exception if the result is null.
/// </summary>
/// <param name="helper">The <see cref="IUrlHelper"/>.</param>
/// <param name="action">The name of the action method.</param>
/// <param name="controller">The name of the controller.</param>
/// <param name="values">An object that contains route values.</param>
/// <param name="protocol">The protocol for the URL, such as "http" or "https".</param>
/// <param name="host">The host name for the URL.</param>
/// <param name="fragment">The fragment for the URL.</param>
/// <returns>The generated URL.</returns>
/// <exception cref="InvalidOperationException">Thrown when URL generation returns null.</exception>
public static string SafeAction(this IUrlHelper helper, string? action, string? controller, object? values, string? protocol, string? host, string? fragment)
{
ArgumentNullException.ThrowIfNull(helper);
var url = helper.Action(new UrlActionContext
{
Action = action,
Controller = controller,
Values = values,
Protocol = protocol,
Host = host,
Fragment = fragment
});
if (url is not null)
{
return url;
}
var actionName = action ?? "<null>";
var controllerName = controller ?? "<null>";
throw new InvalidOperationException($"Failed to generate URL for action '{actionName}' on controller '{controllerName}'.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment