Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store hostnames for current request #986

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions core/Piranha.AspNetCore/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,17 @@ public string ResizeImage(Media image, int width, int? height = null)
public IMediaHelper Media { get; internal set; }

/// <summary>
/// Gets the currently requested URL.
/// Gets/sets the currently requested URL.
/// </summary>
public string Url { get; set; }

/// <summary>
/// Gets the id of the currently requested page.
/// Gets/sets the requested hostname
/// </summary>
public string Hostname { get; set; }

/// <summary>
/// Gets/sets the id of the currently requested page.
/// </summary>
public Guid PageId { get; set; }

Expand Down Expand Up @@ -158,6 +163,8 @@ public ApplicationService(IApi api)
/// </summary>
public async Task InitAsync(HttpContext context)
{
var hostname = context.Request.Host.Host;

// Gets the current site info
if (!context.Request.Path.Value.StartsWith("/manager/"))
{
Expand All @@ -168,10 +175,14 @@ public async Task InitAsync(HttpContext context)
if (!string.IsNullOrEmpty(url) && url.Length > 1)
{
var segments = url.Substring(1).Split(new char[] { '/' });
site = await Api.Sites.GetByHostnameAsync($"{context.Request.Host.Host}/{segments[0]}");
var prefixedHostname = $"{context.Request.Host.Host}/{segments[0]}";
site = await Api.Sites.GetByHostnameAsync(prefixedHostname);

if (site != null)
{
context.Request.Path = "/" + string.Join("/", segments.Skip(1));
hostname = prefixedHostname;
}
}

// Try to get the requested site by hostname
Expand All @@ -193,6 +204,7 @@ public async Task InitAsync(HttpContext context)

// Get the current url
Url = context.Request.Path.Value;
Hostname = hostname;
}
}
}
9 changes: 7 additions & 2 deletions core/Piranha.AspNetCore/Services/IApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ public interface IApplicationService
IMediaHelper Media { get; }

/// <summary>
/// Gets the currently requested URL.
/// Gets/sets the currently requested URL.
/// </summary>
string Url { get; set; }

/// <summary>
/// Gets the id of the currently requested page.
/// Gets/sets the requested hostname
/// </summary>
string Hostname { get; set; }

/// <summary>
/// Gets/sets the id of the currently requested page.
/// </summary>
Guid PageId { get; set; }

Expand Down