Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 1 addition & 1 deletion src/HydroComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void SetupViewContext()

if (view == null)
{
throw new InvalidOperationException($"The view '{GetViewPath()}' was not found.");
throw new HydroException($"The view '{GetViewPath()}' was not found.");
}

_writer = new StringWriter();
Expand Down
19 changes: 19 additions & 0 deletions src/HydroException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Hydro;

/// <summary>
/// Hydro exception
/// </summary>
public class HydroException : Exception
{
/// <inheritdoc />
public HydroException(string message)
: base(message)
{
}

/// <inheritdoc />
public HydroException(string message, Exception innerException)
: base(message, innerException)
{
}
}
27 changes: 25 additions & 2 deletions src/HydroView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
/// </summary>
public T Reference<T>() => default;

/// <summary>
/// Override this property if you want to use custom view path for the component
/// </summary>
public virtual string ViewPath => null;

private async Task<string> GetViewHtml()
{
var services = ViewContext.HttpContext.RequestServices;
Expand All @@ -77,6 +82,11 @@ private async Task<string> GetViewHtml()

var view = GetView(compositeViewEngine, viewType)
?? GetView(compositeViewEngine, viewType, path => path.Replace("TagHelper.cshtml", ".cshtml"));

if (view == null)
{
throw new HydroException($"The view '{GetViewPath(viewType)}' was not found.");
}

await using var writer = new StringWriter();
var viewDataDictionary = new ViewDataDictionary(modelMetadataProvider, ModelState)
Expand Down Expand Up @@ -112,12 +122,25 @@ private async Task UpdateSlots(TagHelperContext context, TagHelperOutput output)
/// </summary>
private IView GetView(IViewEngine viewEngine, Type type, Func<string, string> nameConverter = null)
{
var assemblyName = type.Assembly.GetName().Name;
var path = $"{type.FullName!.Replace(assemblyName!, "~").Replace(".", "/")}.cshtml";
var path = GetViewPath(type);
var adjustedPath = nameConverter != null ? nameConverter(path) : path;
return viewEngine.GetView(null, adjustedPath, false).View;
}

/// <summary>
/// Get the view path based on the type
/// </summary>
protected string GetViewPath(Type type)
{
if (ViewPath != null)
{
return ViewPath;
}

var assemblyName = type.Assembly.GetName().Name;
return $"{type.FullName!.Replace(assemblyName!, "~").Replace(".", "/")}.cshtml";
}

private void ApplyObjectFromDictionary<T>(T target, IDictionary<string, object> source)
{
if (source == null || target == null)
Expand Down