Skip to content

Commit cbb1983

Browse files
author
richard
committed
Wire up DefaultViewLocator
1 parent 37dbd79 commit cbb1983

File tree

9 files changed

+128
-27
lines changed

9 files changed

+128
-27
lines changed

sample/Carter.HtmlNegotiator.Sample.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,12 @@
99
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.0" />
1010
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.0" />
1111
<PackageReference Include="Carter" Version="3.7.0" />
12+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\dependencies\Carter\src\Carter.csproj" />
18+
<ProjectReference Include="..\src\Carter.HtmlNegotiator.csproj" />
1219
</ItemGroup>
1320
</Project>

sample/HomeModule.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

sample/Modules/HomeModule.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Carter.HtmlNegotiator.Sample.Modules
2+
{
3+
using Carter;
4+
using global::HtmlNegotiator;
5+
using Response;
6+
7+
public class HomeModule : CarterModule
8+
{
9+
public HomeModule()
10+
{
11+
Get("/",
12+
async (req, res, routeData) =>
13+
{
14+
await res.Negotiate(new {title = "Hello World!", message = "Hello from Carter.HtmlNegotiator!"});
15+
});
16+
17+
Get("/custom",
18+
async (req, res, routeData) =>
19+
{
20+
await res.View("myView", new {title = "Custom View!", message = "Hello from a custom view name!"});
21+
});
22+
}
23+
}
24+
}

sample/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
namespace Carter.HtmlNegotiator.Sample
22
{
33
using Carter;
4+
using global::HtmlNegotiator;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
68

79
public class Startup
810
{
911
public void ConfigureServices(IServiceCollection services)
1012
{
13+
services.AddHtmlNegotiator();
1114
services.AddCarter();
1215
}
1316

14-
public void Configure(IApplicationBuilder app)
17+
public void Configure(IApplicationBuilder app, ILoggerFactory logging)
1518
{
19+
logging.AddConsole();
20+
logging.AddDebug();
21+
1622
app.UseCarter();
1723
}
1824
}

sample/Views/Home/index.hbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>{{title}}</title>
5+
</head>
6+
<body>
7+
<h1>{{message}}</h1>
8+
</body>
9+
</html>

sample/Views/Home/myView.hbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>{{title}}</title>
5+
</head>
6+
<body>
7+
<h1>{{message}}</h1>
8+
</body>
9+
</html>

src/DefaultViewLocator.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,28 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Linq;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.Http;
89

910
public class DefaultViewLocator : IViewLocator
1011
{
12+
private readonly HtmlNegotiatorOptions options;
1113
private readonly IDictionary<Type, string> mappings;
1214

1315
private readonly IDictionary<Type, string> htmlMappings;
1416

15-
public DefaultViewLocator(IDictionary<Type, string> mappings)
17+
public DefaultViewLocator(HtmlNegotiatorOptions options)
1618
{
17-
this.mappings = mappings;
19+
this.options = options;
20+
this.mappings = new Dictionary<Type, string>();
1821
this.htmlMappings = new Dictionary<Type, string>();
1922
}
2023

2124
public string GetView(object model, HttpContext httpContext)
2225
{
23-
string viewName = string.Empty;
24-
try
25-
{
26-
viewName = this.mappings[model.GetType()];
27-
}
28-
catch (Exception)
29-
{
30-
return string.Empty;
31-
}
26+
string viewName = httpContext.Items.ContainsKey("View") ? httpContext.Items["View"].ToString() : "index.hbs";
27+
string moduleName = "home";
3228

3329
if (this.htmlMappings.ContainsKey(model.GetType()))
3430
{
@@ -37,10 +33,26 @@ public string GetView(object model, HttpContext httpContext)
3733

3834
var env = (IHostingEnvironment)httpContext.RequestServices.GetService(typeof(IHostingEnvironment));
3935

40-
try
36+
if (httpContext.Items.ContainsKey("ModuleType"))
4137
{
42-
var html = File.ReadAllText(Path.Combine(env.ContentRootPath, viewName));
38+
var moduleType = httpContext.Items["ModuleType"] as Type;
39+
if (mappings.ContainsKey(moduleType))
40+
{
41+
moduleName = mappings[moduleType];
42+
}
43+
else
44+
{
45+
var moduleTypeName = moduleType.ToString().Split('.').Last().ToLower();
46+
moduleName = moduleTypeName.Substring(0, moduleTypeName.IndexOf("module"));
47+
mappings.Add(moduleType, moduleName);
48+
}
49+
}
4350

51+
try
52+
{
53+
var path = Path.Combine(env.ContentRootPath, options.ViewLocation, moduleName, viewName);
54+
path = Path.GetExtension(path) == ".hbs" ? path : $"{path}.hbs";
55+
var html = File.ReadAllText(path);
4456
this.htmlMappings.Add(model.GetType(), html);
4557

4658
return html;

src/IServiceCollectionExtentions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace HtmlNegotiator
2+
{
3+
using System;
4+
using Carter;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
public static class IServiceCollectionExtentions
8+
{
9+
public static void AddHtmlNegotiator(this IServiceCollection services)
10+
{
11+
services.AddScoped<IViewLocator>(provider => new DefaultViewLocator(new HtmlNegotiatorOptions()));
12+
services.AddScoped<IResponseNegotiator, HtmlNegotiator>();
13+
}
14+
15+
public static void AddHtmlNegotiator(this IServiceCollection services, Action<HtmlNegotiatorOptions> options)
16+
{
17+
services.Configure(options);
18+
services.AddScoped<IViewLocator, DefaultViewLocator>();
19+
services.AddScoped<IResponseNegotiator, HtmlNegotiator>();
20+
}
21+
}
22+
23+
public class HtmlNegotiatorOptions
24+
{
25+
public HtmlNegotiatorOptions()
26+
{
27+
ViewLocation = "views";
28+
}
29+
30+
public string ViewLocation { get; }
31+
}
32+
}

src/ResponseExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace HtmlNegotiator
2+
{
3+
using System.Threading.Tasks;
4+
using Carter.Response;
5+
using Microsoft.AspNetCore.Http;
6+
7+
public static class ResponseExtensions
8+
{
9+
public static async Task View(this HttpResponse res, string view, object model)
10+
{
11+
res.HttpContext.Items.Add("View", view);
12+
await res.Negotiate(model);
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)