Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is HttpContext.Timestamp in ASP.NET Core MVC?

I would like to get the initial timestamp of the current HTTP request in an ASP.NET Core MVC controller. This timestamp used to be accessible (pre ASP.NET Core) by HttpContext.Timestamp, but Timestamp doesn't seem to be a property of HttpContext anymore.

Where is this property moved to? Or - when it is no longer available - how can I get the timestamp of the HTTP request?

like image 502
crates_barrels Avatar asked Sep 25 '17 16:09

crates_barrels


People also ask

How do I find HttpContext current?

This is useful when you have a common service that is used by your controllers. You can then access the current HTTP context in a safe way: var context = _httpContextAccessor. HttpContext; // Do something with the current HTTP context...

What is HttpContext current in C#?

This property is a static property of the HttpContext class. The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class. You can also use the Page.

What is the use of HttpContext current?

The static property Current on the HttpContext class can be useful whenever the flow of control leaves the code in your Page derived web form. Using this property you can reach out and magically grab the current Request, Response, Session, and Application objects (and more) for the request you are servicing.


1 Answers

You can add your own middleware to the pipeline which adds additional data to the request. For example:

public void Configure(IApplicationBuilder app)
{
    //Make sure this code is placed at the very start to ensure it 
    //executes as soon as possible
    app.Use(async (context, next) =>
    {
        context.Items.Add("RequestStartedOn", DateTime.UtcNow);
        await next();
    };

    //The rest of your code here...
}

Then later on in the pipeline:

var requestStartedOn = (DateTime)httpContext.Items["RequestStartedOn"];

As an aside, if you intend to reuse this code elsewhere, I would put it in it's own library. For example:

public class RequestTimestampMiddleware
{
    private readonly RequestDelegate _next;

    public RequestTimestampMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public Task Invoke(HttpContext context)
    {
        context.Items.Add("RequestStartedOn", DateTime.UtcNow);

        // Call the next delegate/middleware in the pipeline
        return this._next(context);
    }
}

And then add an extension method to make it easy to use:

public static class RequestTimestampMiddlewareExtensions
{
    public static IApplicationBuilder UseRequestTimestamp(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<RequestTimestampMiddleware>();
    }
}

Now your Configure method will look a lot nicer:

public void Configure(IApplicationBuilder app)
{
    app.UseRequestTimestamp();

    //The rest of your code here...
}
like image 77
DavidG Avatar answered Sep 20 '22 00:09

DavidG