forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddPageVirtualPathAttribute.cs
More file actions
43 lines (37 loc) · 1.73 KB
/
AddPageVirtualPathAttribute.cs
File metadata and controls
43 lines (37 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Web;
namespace ServiceStack.Razor.Compilation.CodeTransformers
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class VirtualPathAttribute : Attribute
{
public VirtualPathAttribute( string virtualPath )
{
VirtualPath = virtualPath;
}
public string VirtualPath { get; private set; }
}
public class AddPageVirtualPathAttribute : RazorCodeTransformerBase
{
private const string VirtualPathDirectiveKey = "VirtualPath";
private string _projectRelativePath;
private string _overriddenVirtualPath;
public override void Initialize(RazorPageHost razorHost, IDictionary<string, string> directives)
{
_projectRelativePath = razorHost.File.VirtualPath;
directives.TryGetValue(VirtualPathDirectiveKey, out _overriddenVirtualPath);
}
public override void ProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod)
{
Debug.Assert(_projectRelativePath != null, "Initialize has to be called before we get here.");
var virtualPath = _overriddenVirtualPath ?? VirtualPathUtility.ToAppRelative("~/" + _projectRelativePath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
generatedClass.CustomAttributes.Add(
new CodeAttributeDeclaration(typeof(VirtualPathAttribute).FullName,
new CodeAttributeArgument(new CodePrimitiveExpression(virtualPath))));
}
}
}