Get Absolute URL in Blazor class

Kuler Master 386 Reputation points
2025-01-20T14:00:42.1733333+00:00

Hello,

I have a class inside the Services folder.

It contains a LogError(Exception ex) method in which I need the absolute URL in order to get the requested page.

However, I am unable to access the HttpContext.Request.Url or any other method/extension that I tried.

Thank you!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,745 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,650 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,571 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,217 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,751 Reputation points
    2025-01-20T16:27:25.35+00:00

    Rather than coupling your logging code with ASP.NET a more robust solution could be just pushing any "additional properties" into some global context, like Serilog does with enrichers. Docs

    This code's a bit rough & ready but hopefully puts the point across around how you might achieve this:

    Logger logger = new();
    
    string url = "..."; // Pulled from HttpContext
    
    // With properties:
    using (LogContext.PushProperty("Url", url))
    {
    	logger.LogException(new Exception("Failed (First)!"));
    }
    
    // Without properties:
    logger.LogException(new Exception("Failed (First)!"));
    
    public sealed class Logger
    {
    	public void LogException(Exception e)
    	{
    		var properties = new Dictionary<string, object>
    		{
    			{"Type", e.GetType().ToString()},
    			{"Message", e.Message},
    			{"StackTrace", e.StackTrace}
    		};
    
    		foreach (var property in LogContext.GetProperties())
    		{
    			properties.Add(property.Key, property.Value);
    		}
    
    		// Write somewhere, .e.g.
    		Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(properties));
    	}
    }
    
    public static class LogContext
    {
    	private static readonly List<Property> _properties = new();
    
    	public static Property PushProperty(string key, object value)
    	{
    		var property = new Property { Key = key, Value = value };
    
    		_properties.Add(property);
    
    		return property;
    	}
    
    	public static IEnumerable<Property> GetProperties()
    	{
    		foreach (var property in _properties)
    		{
    			yield return property;
    		}
    	}
    
    	public class Property : IDisposable
    	{
    		public required string Key { get; init; }
    		public required object Value { get; init; }
    
    		public void Dispose()
    		{
    			_properties.Remove(this);
    		}
    	}
    }
    

    If you're familiar with ASP.NET middleware you could create one that pushes all the relevant ASP.NET context properties into LogContext by wrapping the await next.Invoke(); in a using scope like I'm doing above: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-9.0

    0 comments No comments

  2. Bruce (SqlWork.com) 69,806 Reputation points
    2025-01-20T17:21:02.4366667+00:00

    Blazor is a single page app. HttpContext is read only and only contains the url of the page that loaded the Blazor app. To get the navigation url, you inject the NavigationManager, and use it to access the URI.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.