Skip to content

Commit f585bca

Browse files
committed
Add new Razor and Nancy test projects to try getting MVC Razor + intelli-sense working in ServiceStack
1 parent e1d3e86 commit f585bca

65 files changed

Lines changed: 1355 additions & 69 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ServiceStack.Common/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("3.7.3.0")]
36+
[assembly: AssemblyVersion("3.7.4.0")]
3737

3838
// CCB Custom
3939
[assembly: ContractNamespace("http://schemas.servicestack.net/types",

src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Net;
44
using System.Web;
5+
using ServiceStack.Common;
56
using ServiceStack.Logging;
67
using ServiceStack.Service;
78
using ServiceStack.ServiceHost;
@@ -555,7 +556,7 @@ private string GetUrl(string relativeOrAbsoluteUrl)
555556
return relativeOrAbsoluteUrl.StartsWith("http:")
556557
|| relativeOrAbsoluteUrl.StartsWith("https:")
557558
? relativeOrAbsoluteUrl
558-
: this.BaseUri + relativeOrAbsoluteUrl;
559+
: this.BaseUri.CombineWith(relativeOrAbsoluteUrl);
559560
}
560561

561562
#if !SILVERLIGHT

src/ServiceStack.Interfaces/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("3.7.3.0")]
36+
[assembly: AssemblyVersion("3.7.4.0")]
3737

3838
[assembly: ContractNamespace("http://schemas.servicestack.net/types",
3939
ClrNamespace = "ServiceStack.ServiceInterface.ServiceModel")]

src/ServiceStack.RazorEngine/RazorFormat.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ public class RazorFormat : ITemplateResolver, IActivator, IViewEngine, IPlugin,
3333
public static string TemplatePlaceHolder = "@RenderBody()";
3434

3535
// ~/View - Dynamic Pages
36-
public Dictionary<string, RazorPage> ViewPages = new Dictionary<string, RazorPage>(
36+
public Dictionary<string, ViewPage> ViewPages = new Dictionary<string, ViewPage>(
3737
StringComparer.CurrentCultureIgnoreCase);
3838

3939
// ~/View/Shared - Dynamic Shared Pages
40-
public Dictionary<string, RazorPage> ViewSharedPages = new Dictionary<string, RazorPage>(
40+
public Dictionary<string, ViewPage> ViewSharedPages = new Dictionary<string, ViewPage>(
4141
StringComparer.CurrentCultureIgnoreCase);
4242

4343
//Content Pages outside of ~/View
44-
public Dictionary<string, RazorPage> ContentPages = new Dictionary<string, RazorPage>(
44+
public Dictionary<string, ViewPage> ContentPages = new Dictionary<string, ViewPage>(
4545
StringComparer.CurrentCultureIgnoreCase);
4646

47-
public Dictionary<string, RazorPage> PageTemplates = new Dictionary<string, RazorPage>(
47+
public Dictionary<string, ViewPage> PageTemplates = new Dictionary<string, ViewPage>(
4848
StringComparer.CurrentCultureIgnoreCase);
4949

5050
public IAppHost AppHost { get; set; }
5151

5252
public Dictionary<string, string> MarkdownReplaceTokens { get; set; }
5353

54-
public Func<string, IEnumerable<RazorPage>> FindRazorPagesFn { get; set; }
54+
public Func<string, IEnumerable<ViewPage>> FindRazorPagesFn { get; set; }
5555

5656
public RazorFormat()
5757
{
@@ -80,7 +80,7 @@ public void Configure(IAppHost appHost)
8080
appHost.HtmlProviders.Add((requestContext, dto, httpRes) => {
8181

8282
var httpReq = requestContext.Get<IHttpRequest>();
83-
RazorPage razorPage;
83+
ViewPage razorPage;
8484
if ((razorPage = GetViewPageByResponse(dto, httpReq)) == null)
8585
return false;
8686

@@ -90,7 +90,7 @@ public void Configure(IAppHost appHost)
9090
});
9191

9292
appHost.CatchAllHandlers.Add((httpMethod, pathInfo, filePath) => {
93-
RazorPage razorPage;
93+
ViewPage razorPage;
9494
if (filePath == null || (razorPage = GetContentPage(filePath.WithoutExtension())) == null) return null;
9595
return new RazorHandler {
9696
RazorFormat = this,
@@ -118,15 +118,15 @@ public void Init(Type razorBaseType = null)
118118
}
119119
else
120120
{
121-
Razor.SetTemplateBase(typeof(RazorPageBase<>));
121+
Razor.SetTemplateBase(typeof(ViewPage<>));
122122
}
123123

124124
Razor.DefaultTemplateService.RazorFormat = this;
125125
Razor.AddResolver(this);
126126
Razor.SetActivator(this);
127127
}
128128

129-
public IEnumerable<RazorPage> FindRazorPages(string dirPath)
129+
public IEnumerable<ViewPage> FindRazorPages(string dirPath)
130130
{
131131
var di = new DirectoryInfo(dirPath);
132132
var razorFiles = di.GetMatchingFiles("*.cshtml");
@@ -148,7 +148,7 @@ public IEnumerable<RazorPage> FindRazorPages(string dirPath)
148148

149149
var templatePath = GetTemplatePath(fileInfo.DirectoryName);
150150

151-
yield return new RazorPage(this, razorFile, pageName, pageContents, pageType) {
151+
yield return new ViewPage(this, razorFile, pageName, pageContents, pageType) {
152152
TemplatePath = templatePath,
153153
LastModified = fileInfo.LastWriteTime,
154154
};
@@ -183,7 +183,7 @@ private string GetTemplatePath(string fileDirPath)
183183
return null;
184184
}
185185

186-
public bool ProcessRazorPage(IHttpRequest httpReq, RazorPage razorPage, object dto, IHttpResponse httpRes)
186+
public bool ProcessRazorPage(IHttpRequest httpReq, ViewPage razorPage, object dto, IHttpResponse httpRes)
187187
{
188188
httpRes.AddHeaderLastModified(razorPage.GetLastModified());
189189

@@ -202,15 +202,15 @@ public bool ProcessRazorPage(IHttpRequest httpReq, RazorPage razorPage, object d
202202
return true;
203203
}
204204

205-
public void ReloadModifiedPageAndTemplates(RazorPage razorPage)
205+
public void ReloadModifiedPageAndTemplates(ViewPage razorPage)
206206
{
207207
var lastWriteTime = File.GetLastWriteTime(razorPage.FilePath);
208208
if (lastWriteTime > razorPage.LastModified)
209209
{
210210
razorPage.Reload();
211211
}
212212

213-
RazorPage template;
213+
ViewPage template;
214214
if (razorPage.DirectiveTemplatePath != null
215215
&& this.PageTemplates.TryGetValue(razorPage.DirectiveTemplatePath, out template))
216216
{
@@ -227,7 +227,7 @@ public void ReloadModifiedPageAndTemplates(RazorPage razorPage)
227227
}
228228
}
229229

230-
private void ReloadTemplate(RazorPage template)
230+
private void ReloadTemplate(ViewPage template)
231231
{
232232
var contents = File.ReadAllText(template.FilePath);
233233
foreach (var markdownReplaceToken in MarkdownReplaceTokens)
@@ -237,7 +237,7 @@ private void ReloadTemplate(RazorPage template)
237237
template.Reload(contents);
238238
}
239239

240-
private RazorPage GetViewPageByResponse(object dto, IHttpRequest httpRequest)
240+
private ViewPage GetViewPageByResponse(object dto, IHttpRequest httpRequest)
241241
{
242242
var httpResult = dto as IHttpResult;
243243
if (httpResult != null)
@@ -258,9 +258,9 @@ private RazorPage GetViewPageByResponse(object dto, IHttpRequest httpRequest)
258258
return httpRequest != null ? GetViewPage(httpRequest.OperationName) : null;
259259
}
260260

261-
public RazorPage GetViewPage(string pageName)
261+
public ViewPage GetViewPage(string pageName)
262262
{
263-
RazorPage razorPage;
263+
ViewPage razorPage;
264264

265265
ViewPages.TryGetValue(pageName, out razorPage);
266266
if (razorPage != null) return razorPage;
@@ -277,7 +277,7 @@ private void RegisterRazorPages(string razorSearchPath)
277277
}
278278
}
279279

280-
public void AddPage(RazorPage page)
280+
public void AddPage(ViewPage page)
281281
{
282282
try
283283
{
@@ -308,7 +308,7 @@ public void AddPage(RazorPage page)
308308
AddTemplate(templatePath, File.ReadAllText(templatePath));
309309
}
310310

311-
public RazorPage AddTemplate(string templatePath, string templateContents)
311+
public ViewPage AddTemplate(string templatePath, string templateContents)
312312
{
313313
var templateFile = new FileInfo(templatePath);
314314
var templateName = templateFile.FullName.WithoutExtension();
@@ -318,7 +318,7 @@ public RazorPage AddTemplate(string templatePath, string templateContents)
318318
templateContents = templateContents.Replace(markdownReplaceToken.Key, markdownReplaceToken.Value);
319319
}
320320

321-
var template = new RazorPage(this, templatePath, templateName, templateContents, RazorPageType.Template) {
321+
var template = new ViewPage(this, templatePath, templateName, templateContents, RazorPageType.Template) {
322322
LastModified = templateFile.LastWriteTime,
323323
};
324324
PageTemplates.Add(templatePath, template);
@@ -334,17 +334,17 @@ public RazorPage AddTemplate(string templatePath, string templateContents)
334334
}
335335
}
336336

337-
public RazorPage GetContentPage(string pageFilePath)
337+
public ViewPage GetContentPage(string pageFilePath)
338338
{
339-
RazorPage razorPage;
339+
ViewPage razorPage;
340340
ContentPages.TryGetValue(pageFilePath, out razorPage);
341341
return razorPage;
342342
}
343343

344344
public string GetTemplate(string name)
345345
{
346346
Console.WriteLine("GetTemplate(): " + name);
347-
RazorPage template;
347+
ViewPage template;
348348
PageTemplates.TryGetValue(name, out template);
349349
return template != null ? template.Contents : null;
350350
}
@@ -371,14 +371,14 @@ public string RenderStaticPage(string filePath)
371371

372372
filePath = filePath.WithoutExtension();
373373

374-
RazorPage razorPage;
374+
ViewPage razorPage;
375375
if (!ContentPages.TryGetValue(filePath, out razorPage))
376376
throw new InvalidDataException(ErrorPageNotFound.FormatWith(filePath));
377377

378378
return RenderStaticPage(razorPage);
379379
}
380380

381-
private string RenderStaticPage(RazorPage markdownPage)
381+
private string RenderStaticPage(ViewPage markdownPage)
382382
{
383383
var template = ExecuteTemplate((object)null,
384384
markdownPage.PageName, markdownPage.TemplatePath);

src/ServiceStack.RazorEngine/RazorHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ServiceStack.RazorEngine
88
public class RazorHandler : EndpointHandlerBase
99
{
1010
public RazorFormat RazorFormat { get; set; }
11-
public RazorPage RazorPage { get; set; }
11+
public ViewPage RazorPage { get; set; }
1212

1313
public string PathInfo { get; set; }
1414
public string FilePath { get; set; }

src/ServiceStack.RazorEngine/ServiceStack.RazorEngine.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@
103103
<Compile Include="Razor.cs" />
104104
<Compile Include="RazorFormat.cs" />
105105
<Compile Include="RazorHandler.cs" />
106-
<Compile Include="RazorPage.cs" />
107-
<Compile Include="RazorPageBase.cs" />
106+
<Compile Include="ViewPage.cs" />
107+
<Compile Include="ViewPage`1.cs" />
108108
<Compile Include="ServiceStack\MvcWebPageRazorHost.cs" />
109109
<Compile Include="ServiceStack\MvcCSharpRazorCodeParser.cs" />
110+
<Compile Include="ServiceStack\MvcWebRazorHostFactory.cs" />
111+
<Compile Include="ServiceStack\ServiceStackCSharpRazorBuildProvider.cs" />
112+
<Compile Include="ServiceStack\ViewMasterPage.cs" />
113+
<Compile Include="ServiceStack\ViewMasterPage`1.cs" />
110114
<Compile Include="Templating\DefaultActivator.cs" />
111115
<Compile Include="Templating\DelegateActivator.cs" />
112116
<Compile Include="Templating\DelegateTemplateResolver.cs" />

src/ServiceStack.RazorEngine/ServiceStack/MvcWebPageRazorHost.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Diagnostics;
1616
using System.Web.Razor.Generator;
1717
using System.Web.Razor.Parser;
18+
using ServiceStack.MiniProfiler;
1819
using CSharpRazorCodeGenerator = ServiceStack.RazorEngine.Compilation.CSharp.CSharpRazorCodeGenerator;
1920

2021
namespace ServiceStack.RazorEngine.ServiceStack
@@ -41,6 +42,14 @@ public MvcWebPageRazorHost(RazorCodeLanguage codeLanguage, Func<MarkupParser> ma
4142
private void Init()
4243
{
4344
GetRidOfNamespace("System.Web.WebPages.Html");
45+
46+
this.DefaultBaseClass = typeof(ViewPage).FullName;
47+
this.DefaultNamespace = "RazorOutput";
48+
this.DefaultClassName = "RazorView";
49+
50+
this.GeneratedClassContext = new GeneratedClassContext(
51+
"Execute", "Write", "WriteLiteral", "WriteTo", "WriteLiteralTo",
52+
typeof(HelperResult).FullName, "DefineSection");
4453
}
4554

4655
public override RazorCodeGenerator DecorateCodeGenerator(RazorCodeGenerator incomingCodeGenerator)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2+
3+
//using System.Web.Mvc.Razor;
4+
//using System.Web.WebPages.Razor;
5+
//using ServiceStack.RazorEngine.ServiceStack;
6+
7+
//namespace System.Web.Mvc
8+
//{
9+
// public class MvcWebRazorHostFactory : WebRazorHostFactory
10+
// {
11+
// public override WebPageRazorHost CreateHost(string virtualPath, string physicalPath)
12+
// {
13+
// WebPageRazorHost host = base.CreateHost(virtualPath, physicalPath);
14+
15+
// if (!host.IsSpecialPage)
16+
// {
17+
// return new MvcWebPageRazorHost(virtualPath, physicalPath);
18+
// }
19+
20+
// return host;
21+
// }
22+
// }
23+
//}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//Orignally from: https://github.com/NancyFx/Nancy/blob/master/src/Nancy.ViewEngines.Razor.BuildProviders/NancyCSharpRazorBuildProvider.cs
2+
namespace ServiceStack.RazorEngine.ServiceStack
3+
{
4+
using System;
5+
using System.CodeDom;
6+
using System.CodeDom.Compiler;
7+
using System.Globalization;
8+
using System.Web.Compilation;
9+
using System.Web.Razor;
10+
11+
[BuildProviderAppliesTo(BuildProviderAppliesTo.Code | BuildProviderAppliesTo.Web)]
12+
public class ServiceStackCSharpRazorBuildProvider : BuildProvider
13+
{
14+
private readonly RazorEngineHost host;
15+
16+
private readonly CompilerType compilerType;
17+
18+
private CodeCompileUnit generatedCode;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="ServiceStackCSharpRazorBuildProvider"/> class.
22+
/// </summary>
23+
public ServiceStackCSharpRazorBuildProvider()
24+
{
25+
this.compilerType = this.GetDefaultCompilerTypeForLanguage("C#");
26+
27+
this.host = new RazorEngineHost(new CSharpRazorCodeLanguage()) {
28+
DefaultBaseClass = typeof(ViewPage).FullName,
29+
DefaultNamespace = "RazorOutput",
30+
DefaultClassName = "ViewPage"
31+
};
32+
}
33+
34+
/// <summary>
35+
/// Represents the compiler type used by a build provider to generate source code for a custom file type.
36+
/// </summary>
37+
/// <returns>A read-only <see cref="T:System.Web.Compilation.CompilerType"/> representing the code generator, code compiler, and compiler settings used to build source code for the virtual path. The base class returns null.</returns>
38+
public override CompilerType CodeCompilerType
39+
{
40+
get { return this.compilerType; }
41+
}
42+
43+
/// <summary>
44+
/// Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder.
45+
/// </summary>
46+
/// <param name="assemblyBuilder">The assembly builder that references the source code generated by the build provider.</param>
47+
public override void GenerateCode(AssemblyBuilder assemblyBuilder)
48+
{
49+
assemblyBuilder.AddCodeCompileUnit(this, this.GetGeneratedCode());
50+
51+
assemblyBuilder.GenerateTypeFactory(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { this.host.DefaultNamespace, this.host.DefaultClassName }));
52+
}
53+
54+
/// <summary>
55+
/// Returns a type generated by the build provider from the virtual path.
56+
/// </summary>
57+
/// <returns>The type that is generated by the build provider for the virtual path. The base class returns null.</returns>
58+
/// <param name="results">The compilation results for the build provider's virtual path.</param>
59+
public override Type GetGeneratedType(CompilerResults results)
60+
{
61+
return results.CompiledAssembly.GetType(string.Format(CultureInfo.CurrentCulture, "{0}.{1}", new object[] { this.host.DefaultNamespace, this.host.DefaultClassName }));
62+
}
63+
64+
private CodeCompileUnit GetGeneratedCode()
65+
{
66+
if (this.generatedCode == null)
67+
{
68+
var engine = new RazorTemplateEngine(this.host);
69+
GeneratorResults results;
70+
using (var reader = this.OpenReader())
71+
{
72+
results = engine.GenerateCode(reader);
73+
}
74+
75+
if (!results.Success)
76+
{
77+
throw new InvalidOperationException(results.ToString());
78+
}
79+
80+
this.generatedCode = results.GeneratedCode;
81+
}
82+
83+
return this.generatedCode;
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)