Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial revision of view engine

  • Loading branch information...
commit 858661f90536ae21899516d926a454b8052cce9d 1 parent 822f0ac
@aaronpowell authored
View
14 Owin.HelloWorld/Owin.HelloWorld.csproj
@@ -69,8 +69,15 @@
<Reference Include="Owin">
<HintPath>..\packages\Owin.0.7\lib\net40\Owin.dll</HintPath>
</Reference>
+ <Reference Include="RazorEngine">
+ <HintPath>..\packages\RazorEngine.3.0.8\lib\net40\RazorEngine.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+ <Private>True</Private>
+ <HintPath>..\packages\RazorEngine.3.0.8\lib\net40\System.Web.Razor.dll</HintPath>
+ </Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
@@ -86,12 +93,19 @@
<Compile Include="Routing\RouteBuilder.cs" />
<Compile Include="Routing\RoutedRequest.cs" />
<Compile Include="Startup.cs" />
+ <Compile Include="ViewEngine.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
+ <None Include="Views\Basic.cshtml">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ <None Include="Views\Model.cshtml">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
View
8 Owin.HelloWorld/Startup.cs
@@ -32,6 +32,14 @@ public static void Configuration(IAppBuilder builder)
{
res.Json(new { Name = req.UrlSegments.name });
})
+ .Get("/razor/basic", (req, res) =>
+ {
+ res.View("Basic");
+ })
+ .Get("/razor/model/:name", (req, res) =>
+ {
+ res.View("Model", new { Name = req.UrlSegments.name });
+ })
.Get((req, res) =>
{
res.Text("Well nothing else matched, that's sad :(\r\n");
View
55 Owin.HelloWorld/ViewEngine.cs
@@ -0,0 +1,55 @@
+using System;
+using System.IO;
+using Gate;
+using RazorEngine;
+
+namespace Owin.HelloWorld
+{
+ public static class ViewEngine
+ {
+ private static FileInfo FindView(string view)
+ {
+ if (string.IsNullOrEmpty(view))
+ throw new ArgumentNullException("view", "View name is required");
+
+ var root = AppDomain.CurrentDomain.BaseDirectory;
+
+ var viewsPath = Path.Combine(root, "views");
+
+ var file = new FileInfo(Path.Combine(viewsPath, view + ".cshtml"));
+ if (!file.Exists)
+ file = new FileInfo(Path.Combine(viewsPath, view + ".vbhtml"));
+
+ if (!file.Exists)
+ throw new FileNotFoundException("No view with the name '" + view + "' was found in the views folder (" + viewsPath + ").\r\nEnsure that you have a file with that name and an extension of either cshtml or vbhtml");
+
+ return file;
+ }
+
+ public static void View(this Response res, string view)
+ {
+ var file = FindView(view);
+
+ var raw = File.ReadAllText(file.FullName);
+
+ var output = Razor.Parse(raw);
+
+ res.ContentType = "text/html";
+ res.Status = "200 OK";
+ res.End(output);
+ }
+
+ public static void View<T>(this Response res, string view, T model)
+ {
+ var file = FindView(view);
+
+ var raw = File.ReadAllText(file.FullName);
+
+ var output = Razor.Parse(raw, model);
+
+ res.ContentType = "text/html";
+ res.Status = "200 OK";
+ res.End(output);
+ }
+ }
+}
View
9 Owin.HelloWorld/Views/Basic.cshtml
@@ -0,0 +1,9 @@
+<!doctype html>
+<html>
+ <head>
+ <title>Hello World!</title>
+ </head>
+ <body>
+ <h1>Hello World, I came from Razor!</h1>
+ </body>
+</html>
View
10 Owin.HelloWorld/Views/Model.cshtml
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <title>Hi @Model.Name</title>
+</head>
+<body>
+ <h1>Hi there @Model.Name. Did you know you're a model property?</h1>
+</body>
+</html>
View
1  Owin.HelloWorld/packages.config
@@ -5,4 +5,5 @@
<package id="Gate.Builder" version="0.3.4" />
<package id="Newtonsoft.Json" version="4.0.8" />
<package id="Owin" version="0.7" />
+ <package id="RazorEngine" version="3.0.8" />
</packages>
Please sign in to comment.
Something went wrong with that request. Please try again.