2020-04-20 12:20:47 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-04-22 13:08:43 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
2020-04-20 12:20:47 +02:00
|
|
|
|
using Umbraco.Web.Common.Extensions;
|
2020-04-22 13:08:43 +02:00
|
|
|
|
using Umbraco.Web.Common.Lifetime;
|
2020-04-20 12:20:47 +02:00
|
|
|
|
using Umbraco.Web.Routing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Common.AspNetCore
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AspNetCoreRequestAccessor : IRequestAccessor
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2020-04-22 13:08:43 +02:00
|
|
|
|
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
public AspNetCoreRequestAccessor(IHttpContextAccessor httpContextAccessor, IUmbracoRequestLifetime umbracoRequestLifetime, IUmbracoContextAccessor umbracoContextAccessor)
|
2020-04-20 12:20:47 +02:00
|
|
|
|
{
|
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2020-04-22 13:08:43 +02:00
|
|
|
|
_umbracoContextAccessor = umbracoContextAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
umbracoRequestLifetime.RequestStart += RequestStart;
|
|
|
|
|
|
umbracoRequestLifetime.RequestEnd += RequestEnd;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void RequestEnd(object sender, HttpContext e)
|
|
|
|
|
|
{
|
|
|
|
|
|
EndRequest?.Invoke(sender, new UmbracoRequestEventArgs(_umbracoContextAccessor.UmbracoContext));
|
2020-04-20 12:20:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-22 13:08:43 +02:00
|
|
|
|
private void RequestStart(object sender, HttpContext e)
|
|
|
|
|
|
{
|
|
|
|
|
|
var reason = EnsureRoutableOutcome.IsRoutable;
|
|
|
|
|
|
RouteAttempt?.Invoke(sender, new RoutableAttemptEventArgs(reason, _umbracoContextAccessor.UmbracoContext));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-20 12:20:47 +02:00
|
|
|
|
public string GetRequestValue(string name) => GetFormValue(name) ?? GetQueryStringValue(name);
|
|
|
|
|
|
public string GetFormValue(string name) => _httpContextAccessor.GetRequiredHttpContext().Request.Form[name];
|
|
|
|
|
|
|
|
|
|
|
|
public string GetQueryStringValue(string name) => _httpContextAccessor.GetRequiredHttpContext().Request.Query[name];
|
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler<UmbracoRequestEventArgs> EndRequest;
|
|
|
|
|
|
|
|
|
|
|
|
//TODO implement
|
|
|
|
|
|
public event EventHandler<RoutableAttemptEventArgs> RouteAttempt;
|
2020-04-22 13:08:43 +02:00
|
|
|
|
public Uri GetRequestUrl() => _httpContextAccessor.HttpContext != null ? new Uri(_httpContextAccessor.HttpContext.Request.GetEncodedUrl()) : null;
|
2020-04-20 12:20:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|