Permalink
Browse files

More MVC Razor refactoring and tweaks...

  • Loading branch information...
1 parent 50eaf5d commit 47cccb00c60e69466d0552d5254635122cebae3a @mythz mythz committed Jun 27, 2011
Showing with 193 additions and 150 deletions.
  1. BIN NuGet/ServiceStack.2.93.nupkg
  2. BIN NuGet/ServiceStack.2.95.nupkg
  3. BIN NuGet/ServiceStack.2.96.nupkg
  4. BIN NuGet/ServiceStack/lib/RazorEngine.dll
  5. BIN NuGet/ServiceStack/lib/ServiceStack.Common.dll
  6. BIN NuGet/ServiceStack/lib/ServiceStack.Interfaces.dll
  7. BIN NuGet/ServiceStack/lib/ServiceStack.ServiceInterface.dll
  8. BIN NuGet/ServiceStack/lib/ServiceStack.Text.dll
  9. BIN NuGet/ServiceStack/lib/ServiceStack.dll
  10. BIN NuGet/ServiceStack/lib/System.Web.Razor.dll
  11. +1 −1 NuGet/ServiceStack/servicestack.nuget
  12. +1 −3 build/build.bat
  13. +3 −1 src/RazorEngine/IRazorTemplate.cs
  14. +1 −2 src/RazorEngine/RazorEngine.csproj
  15. +29 −22 src/RazorEngine/{MvcRazorFormat.cs → RazorFormat.cs}
  16. +4 −4 src/RazorEngine/RazorHandler.cs
  17. +3 −3 src/RazorEngine/RazorPage.cs
  18. +8 −1 src/RazorEngine/RazorPageBase.cs
  19. +3 −8 src/RazorEngine/Templating/TemplateService.ServiceStack.cs
  20. +6 −0 src/ServiceStack.Common/Web/HttpResponseStreamWrapper.cs
  21. +2 −0 src/ServiceStack.Interfaces/ServiceHost/IHttpResponse.cs
  22. +1 −0 src/ServiceStack/WebHost.EndPoints/EndpointHostConfig.cs
  23. +6 −0 src/ServiceStack/WebHost.EndPoints/Extensions/HttpListenerResponseWrapper.cs
  24. +6 −0 src/ServiceStack/WebHost.EndPoints/Extensions/HttpResponseWrapper.cs
  25. +13 −5 src/ServiceStack/WebHost.EndPoints/Support/Mocks/HttpResponseMock.cs
  26. 0 tests/ServiceStack.ServiceHost.Tests/AppData/Template/{default.cshtml → _Layout.cshtml}
  27. +31 −23 tests/ServiceStack.ServiceHost.Tests/Formats/MarkdownTestBase.cs
  28. +5 −0 tests/ServiceStack.ServiceHost.Tests/Formats/ViewTests.cs
  29. +1 −1 tests/ServiceStack.ServiceHost.Tests/Formats_Razor/IntroductionExampleRazorTests.cs
  30. +2 −2 tests/ServiceStack.ServiceHost.Tests/Formats_Razor/IntroductionLayoutRazorTests.cs
  31. +0 −63 tests/ServiceStack.ServiceHost.Tests/Formats_Razor/MarkdownTestBase.cs
  32. +1 −1 tests/ServiceStack.ServiceHost.Tests/Formats_Razor/RazorEngineTests.cs
  33. +55 −0 tests/ServiceStack.ServiceHost.Tests/Formats_Razor/RazorTestBase.cs
  34. +6 −6 tests/ServiceStack.ServiceHost.Tests/Formats_Razor/TemplateTests.cs
  35. +4 −4 tests/ServiceStack.ServiceHost.Tests/ServiceStack.ServiceHost.Tests.csproj
  36. 0 tests/ServiceStack.ServiceHost.Tests/Views/Template/{default.cshtml → _Layout.cshtml}
  37. +1 −0 tests/ServiceStack.WebHost.Endpoints.Tests/IntegrationTests/ResetMovies.cs
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>ServiceStack</id>
- <version>2.92</version>
+ <version>2.96</version>
<authors>Demis Bellot</authors>
<owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary>
View
@@ -5,9 +5,7 @@ SET BUILD=Release
%MSBUILD% build.msbuild
-COPY ..\src\ServiceStack\bin\%BUILD%\*.* ..\NuGet\ServiceStack\lib
-COPY ..\src\RazorEngine\bin\%BUILD%\RazorEngine.dll ..\NuGet\ServiceStack\lib\Net40
-COPY ..\src\RazorEngine\bin\%BUILD%\System.Web.Razor.dll ..\NuGet\ServiceStack\lib\Net40
+COPY ..\src\RazorEngine\bin\%BUILD%\*.* ..\NuGet\ServiceStack\lib
COPY ..\src\ServiceStack.ServiceInterface\bin\%BUILD%\*.* ..\NuGet\ServiceStack\lib
COPY ..\src\RazorEngine\bin\%BUILD%\*.* ..\..\ServiceStack.Examples\lib
@@ -2,15 +2,17 @@
using System.Collections.Generic;
using RazorEngine.Templating;
using ServiceStack.Markdown;
+using ServiceStack.ServiceHost;
namespace RazorEngine
{
public interface IRazorTemplate : ITemplate, ITemplatePage
{
- void Init(IViewEngine viewEngine, ViewDataDictionary viewData);
+ void Init(IViewEngine viewEngine, ViewDataDictionary viewData, IHttpResponse httpRes);
string Layout { get; }
Dictionary<string, Action> Sections { get; }
IRazorTemplate ChildTemplate { get; set; }
void Prepend(string contents);
+ IHttpResponse Response { get; }
}
}
@@ -60,7 +60,6 @@
<Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\System.Web.Razor.dll</HintPath>
- <Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@@ -94,7 +93,7 @@
<Compile Include="Language.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Razor.cs" />
- <Compile Include="MvcRazorFormat.cs" />
+ <Compile Include="RazorFormat.cs" />
<Compile Include="RazorHandler.cs" />
<Compile Include="RazorPage.cs" />
<Compile Include="RazorPageBase.cs" />
@@ -3,6 +3,7 @@
using System.Configuration;
using System.IO;
using System.Linq;
+using System.Reflection;
using RazorEngine.Templating;
using ServiceStack.Common;
using ServiceStack.Common.Utils;
@@ -22,13 +23,15 @@ public enum RazorPageType
Template = 4,
}
- public class MvcRazorFormat : ITemplateResolver, IActivator, IViewEngine
+ public class RazorFormat : ITemplateResolver, IActivator, IViewEngine
{
- private static readonly ILog Log = LogManager.GetLogger(typeof(MvcRazorFormat));
+ private static readonly ILog Log = LogManager.GetLogger(typeof(RazorFormat));
+
+ public static RazorFormat Instance = new RazorFormat();
private const string ErrorPageNotFound = "Could not find Razor page '{0}'";
- public static string TemplateName = "default.cshtml";
+ public static string TemplateName = "_Layout.cshtml";
public static string TemplatePlaceHolder = "@RenderBody()";
// ~/View - Dynamic Pages
@@ -52,13 +55,18 @@ public class MvcRazorFormat : ITemplateResolver, IActivator, IViewEngine
public Func<string, IEnumerable<RazorPage>> FindRazorPagesFn { get; set; }
- public MvcRazorFormat()
+ public RazorFormat()
{
this.FindRazorPagesFn = FindRazorPages;
this.MarkdownReplaceTokens = new Dictionary<string, string>();
}
- public void Register(IAppHost appHost)
+ public static void Register(IAppHost appHost)
+ {
+ Instance.Configure(appHost);
+ }
+
+ public void Configure(IAppHost appHost)
{
this.AppHost = appHost;
this.MarkdownReplaceTokens = new Dictionary<string, string>(appHost.Config.MarkdownReplaceTokens);
@@ -87,7 +95,7 @@ public void Register(IAppHost appHost)
RazorPage razorPage;
if (filePath == null || (razorPage = GetContentPage(filePath.WithoutExtension())) == null) return null;
return new RazorHandler {
- MvcRazorFormat = this,
+ RazorFormat = this,
RazorPage = razorPage,
RequestName = "RazorPage",
PathInfo = pathInfo,
@@ -181,27 +189,21 @@ public bool ProcessRazorPage(IHttpRequest httpReq, RazorPage razorPage, object d
{
httpRes.AddHeaderLastModified(razorPage.GetLastModified());
- var renderInTemplate = true;
+ var templatePath = razorPage.TemplatePath;
if (httpReq != null && httpReq.QueryString["format"] != null)
{
- renderInTemplate = !httpReq.GetFormatModifier().StartsWithIgnoreCase("bare");
+ if (!httpReq.GetFormatModifier().StartsWithIgnoreCase("bare"))
+ templatePath = null;
}
- var markup = RenderDynamicPage(razorPage, razorPage.Name, dto, renderInTemplate);
- var markupBytes = markup.ToUtf8Bytes();
- httpRes.OutputStream.Write(markupBytes, 0, markupBytes.Length);
+ var template = ExecuteTemplate(dto, razorPage.PageName, templatePath, httpRes);
+ var html = template.Result;
+ var htmlBytes = html.ToUtf8Bytes();
+ httpRes.OutputStream.Write(htmlBytes, 0, htmlBytes.Length);
return true;
}
- private string RenderDynamicPage(RazorPage razorPage, string pageName, object model, bool renderTemplate)
- {
- if (razorPage == null)
- throw new InvalidDataException(ErrorPageNotFound.FormatWith(pageName));
-
- return Razor.Run(model, razorPage.PageName);
- }
-
public void ReloadModifiedPageAndTemplates(RazorPage razorPage)
{
var lastWriteTime = File.GetLastWriteTime(razorPage.FilePath);
@@ -380,15 +382,20 @@ public string RenderStaticPage(string filePath)
private string RenderStaticPage(RazorPage markdownPage)
{
- var template = ExecuteTemplate((object)null,
+ var template = ExecuteTemplate((object)null,
markdownPage.PageName, markdownPage.TemplatePath);
return template.Result;
}
public IRazorTemplate ExecuteTemplate<T>(T model, string name, string templatePath)
- {
- return Razor.DefaultTemplateService.ExecuteTemplate(model, name, templatePath);
+ {
+ return ExecuteTemplate(model, name, templatePath, null);
+ }
+
+ public IRazorTemplate ExecuteTemplate<T>(T model, string name, string templatePath, IHttpResponse httpRes)
+ {
+ return Razor.DefaultTemplateService.ExecuteTemplate(model, name, templatePath, httpRes);
}
public string RenderPartial(string pageName, object model, bool renderHtml)
@@ -7,7 +7,7 @@ namespace RazorEngine
{
public class RazorHandler : EndpointHandlerBase
{
- public MvcRazorFormat MvcRazorFormat { get; set; }
+ public RazorFormat RazorFormat { get; set; }
public RazorPage RazorPage { get; set; }
public string PathInfo { get; set; }
@@ -19,20 +19,20 @@ public override void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes,
if (contentPage == null)
{
var pageFilePath = this.FilePath.WithoutExtension();
- contentPage = MvcRazorFormat.GetContentPage(pageFilePath);
+ contentPage = RazorFormat.GetContentPage(pageFilePath);
}
if (contentPage == null)
{
httpRes.StatusCode = (int)HttpStatusCode.NotFound;
return;
}
- MvcRazorFormat.ReloadModifiedPageAndTemplates(contentPage);
+ RazorFormat.ReloadModifiedPageAndTemplates(contentPage);
if (httpReq.DidReturn304NotModified(contentPage.GetLastModified(), httpRes))
return;
- MvcRazorFormat.ProcessRazorPage(httpReq, contentPage, null, httpRes);
+ RazorFormat.ProcessRazorPage(httpReq, contentPage, null, httpRes);
}
public override object CreateRequest(IHttpRequest request, string operationName)
@@ -8,7 +8,7 @@ namespace RazorEngine
{
public class RazorPage
{
- public MvcRazorFormat RazorFormat { get; set; }
+ public RazorFormat RazorFormat { get; set; }
public string FilePath { get; set; }
public string Name { get; set; }
@@ -27,12 +27,12 @@ public RazorPage()
this.Dependents = new List<IExpirable>();
}
- public RazorPage(MvcRazorFormat razorFormat, string fullPath, string name, string contents)
+ public RazorPage(RazorFormat razorFormat, string fullPath, string name, string contents)
: this(razorFormat, fullPath, name, contents, RazorPageType.ViewPage)
{
}
- public RazorPage(MvcRazorFormat razorFormat, string fullPath, string name, string contents, RazorPageType pageType)
+ public RazorPage(RazorFormat razorFormat, string fullPath, string name, string contents, RazorPageType pageType)
: this()
{
RazorFormat = razorFormat;
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using RazorEngine.Templating;
using ServiceStack.Markdown;
+using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
namespace RazorEngine
@@ -12,11 +13,13 @@ public abstract class RazorPageBase<TModel> : TemplateBase<TModel>, IRazorTempla
public HtmlHelper<TModel> Html = new HtmlHelper<TModel>();
public IAppHost AppHost { get; set; }
+ public IHttpResponse Response { get; set; }
public Dictionary<string, object> ScopeArgs { get; set; }
- public void Init(IViewEngine viewEngine, ViewDataDictionary viewData)
+ public void Init(IViewEngine viewEngine, ViewDataDictionary viewData, IHttpResponse httpRes)
{
+ this.Response = httpRes;
Html.Init(viewEngine, viewData);
}
@@ -38,5 +41,9 @@ public T TryResolve<T>()
return this.AppHost.TryResolve<T>();
}
+ public string Href(string url)
+ {
+ return Url.Content(url);
+ }
}
}
@@ -75,17 +75,12 @@ public string RenderSection(string sectionName, bool required)
public partial class TemplateService
{
- public MvcRazorFormat RazorFormat { get; set; }
+ public RazorFormat RazorFormat { get; set; }
/// <summary>
/// Runs and returns the template with the specified name.
/// </summary>
- public IRazorTemplate ExecuteTemplate<T>(T model, string name)
- {
- return ExecuteTemplate(model, name, null);
- }
-
- public IRazorTemplate ExecuteTemplate<T>(T model, string name, string templatePath)
+ public IRazorTemplate ExecuteTemplate<T>(T model, string name, string templatePath=null, IHttpResponse httpRes=null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("The named of the cached template is required.");
@@ -98,7 +93,7 @@ public IRazorTemplate ExecuteTemplate<T>(T model, string name, string templatePa
SetModel(instance, model);
var razorTemplate = (IRazorTemplate)instance;
- razorTemplate.Init(RazorFormat, new ViewDataDictionary<T>(model));
+ razorTemplate.Init(RazorFormat, new ViewDataDictionary<T>(model), httpRes);
instance.Execute();
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
+using ServiceStack.Common.Utils;
using ServiceStack.ServiceHost;
namespace ServiceStack.Common.Web
@@ -24,6 +25,11 @@ public void AddHeader(string name, string value)
this.Headers[name] = value;
}
+ public void Redirect(string url)
+ {
+ this.Headers[HttpHeaders.Location] = url;
+ }
+
public Stream OutputStream { get; private set; }
public void Write(string text)
@@ -10,6 +10,8 @@ public interface IHttpResponse
void AddHeader(string name, string value);
+ void Redirect(string url);
+
Stream OutputStream { get; }
void Write(string text);
@@ -61,6 +61,7 @@ public static EndpointHostConfig Instance
DefaultDocuments = new List<string> {
"default.htm",
"default.html",
+ "default.cshtml",
"index.htm",
"index.html",
"default.aspx",
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Net;
+using ServiceStack.Common.Web;
using ServiceStack.Logging;
using ServiceStack.ServiceHost;
@@ -34,6 +35,11 @@ public void AddHeader(string name, string value)
response.AddHeader(name, value);
}
+ public void Redirect(string url)
+ {
+ response.Redirect(url);
+ }
+
public Stream OutputStream
{
get { return response.OutputStream; }
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Web;
+using ServiceStack.Common.Web;
using ServiceStack.Logging;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
@@ -40,6 +41,11 @@ public void AddHeader(string name, string value)
response.AddHeader(name, value);
}
+ public void Redirect(string url)
+ {
+ response.Redirect(url);
+ }
+
public Stream OutputStream
{
get { return response.OutputStream; }
Oops, something went wrong.

0 comments on commit 47cccb0

Please sign in to comment.