//******************************************************************************************************
// HtmlHelper.cs - Gbtc
//
// Copyright © 2016, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 08/04/2016 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web;
using GSF.IO;
using RazorEngine.Templating;
using RazorEngine.Text;
namespace GSF.Web.Model
{
///
/// Defines an HTML helper class for views.
///
///
/// This class provides Razor view compatibility functions so that the same view syntax, e.g.,
/// @Html.Raw(""), can be used in both self-hosted and ASP.NET hosted web sites.
///
public class HtmlHelper
{
#region [ Members ]
// Fields
private readonly TemplateBase m_parent;
#endregion
#region [ Constructors ]
// Creates a new HTML helper class associated with parent template
internal HtmlHelper(TemplateBase parent)
{
m_parent = parent;
}
#endregion
#region [ Methods ]
///
/// Returns a string value that is not HTML encoded.
///
/// Raw string value to return.
/// Raw string value that is not HTML encoded.
public IEncodedString Raw(string value) => new RawString(value);
///
/// Includes the template with the specified name.
///
/// The name of the template type in cache.
/// The model or NULL if there is no model for the template.
///
/// The template writer helper.
public TemplateWriter Partial(string name, object model = null, Type modelType = null)
{
return m_parent.Include(name, model, modelType);
}
///
/// Converts relative URL to an absolute URL.
///
/// Relative URL.
/// Request message.
/// Absolute URL.
///
/// If HttpRequestMessage is not specified, function will attempt to find message in ViewBag.Request.
///
public string ToAbsoluteUrl(string url, HttpRequestMessage request = null)
{
if (string.IsNullOrEmpty(url))
return "";
if ((object)request == null)
request = m_parent.ViewBag.Request;
if ((object)request == null)
throw new ArgumentNullException(nameof(request));
Uri uri = new Uri(request.RequestUri.AbsoluteUri.Replace(request.RequestUri.PathAndQuery, ""));
string port = uri.Port == 80 ? "" : ":" + uri.Port;
return $"{uri.Scheme}://{uri.Host}{port}{VirtualPathUtility.ToAbsolute(url)}";
}
///
/// Includes an HTTP resource directly into view page.
///
/// URL of resource to include.
/// Resolved URL resource as an encoded string.
public IEncodedString IncludeUrl(string url)
{
if (string.IsNullOrEmpty(url))
return new RawString("");
HttpWebRequest request = WebRequest.CreateHttp(url);
request.Credentials = CredentialCache.DefaultCredentials;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (Stream stream = response?.GetResponseStream())
{
if ((object)stream == null)
return new RawString($"");
using (StreamReader reader = new StreamReader(stream))
return new RawString(reader.ReadToEnd());
}
}
///
/// Includes an embedded resource directly into view page.
///
/// Resource to include.
/// Resource as an encoded string.
public IEncodedString IncludeResouce(string resourceName)
{
if (string.IsNullOrEmpty(resourceName))
return new RawString("");
Stream stream = WebExtensions.OpenEmbeddedResourceStream(resourceName);
if ((object)stream == null)
return new RawString("");
using (StreamReader reader = new StreamReader(stream))
return new RawString(reader.ReadToEnd());
}
///
/// Renders a Razor view from an embedded resource.
///
/// Embedded resource name.
/// Type of model.
/// Model instance.
/// Rendered resource as an encoded string.
public IEncodedString RenderResource(string resourceName, Type modelType = null, object model = null)
{
if (FilePath.GetExtension(resourceName).Equals(".vbhtml", StringComparison.OrdinalIgnoreCase))
return new RawString(RazorEngine.Default.RunCompile(resourceName, modelType, model, m_parent.ViewBag as DynamicViewBag));
return new RawString(RazorEngine.Default.RunCompile(resourceName, modelType, model, m_parent.ViewBag as DynamicViewBag));
}
///
/// Renders a Razor view from an embedded resource.
///
/// Type of model.
/// Embedded resource name.
/// Model instance.
/// Rendered resource as an encoded string.
public IEncodedString RenderResource(string resourceName, T model)
{
return RenderResource(resourceName, typeof(T), model);
}
#endregion
}
}