本文整理匯總了C#中System.Web.Razor.RazorEngineHost類的典型用法代碼示例。如果您正苦於以下問題:C# RazorEngineHost類的具體用法?C# RazorEngineHost怎麽用?C# RazorEngineHost使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
RazorEngineHost類屬於System.Web.Razor命名空間,在下文中一共展示了RazorEngineHost類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: parse
public static ParsedTemplate parse(string sourceFilename, string cshtmlContent, string effectiveTemplateClassName, Type modelType)
{
var csCodeLanguage = new CSharpRazorCodeLanguage();
var templateHost = new RazorEngineHost(csCodeLanguage, () => new HtmlMarkupParser());
var concreteBaseClassType = getBaseClassTypeFromModel(modelType);
templateHost.DefaultBaseClass = concreteBaseClassType.FullName;
var templateEngine = new RazorTemplateEngine(templateHost);
var trimmedcshtmlContent = HeaderLines.trim(cshtmlContent);
GeneratorResults res;
using (var input = new StringReader(trimmedcshtmlContent))
{
res = templateEngine.GenerateCode(input, effectiveTemplateClassName, GeneratedTemplateNamespace, sourceFilename);
}
if (!res.Success)
throw new Exception("Failed to generate code");
var compileUnit = res.GeneratedCode;
var fullyQualifiedClassName = GeneratedTemplateNamespace + "." + effectiveTemplateClassName;
return new ParsedTemplate(fullyQualifiedClassName, compileUnit);
}
示例2: BackgroundParser
public BackgroundParser(RazorEngineHost host, string fileName)
{
_main = new MainThreadState(fileName);
_bg = new BackgroundThread(_main, host, fileName);
_main.ResultsReady += (sender, args) => OnResultsReady(args);
}
示例3: SimpleCSharpRazorCodeGenerator
public SimpleCSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host)
: base(className, rootNamespaceName, sourceFileName, host)
{
var baseType = new CodeTypeReference(SimpleRazorConfiguration.BaseClass);
Context.GeneratedClass.BaseTypes.Clear();
Context.GeneratedClass.BaseTypes.Add(baseType);
}
示例4: SimpleCSharpRazorCodeGenerator
public SimpleCSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host)
: base(className, rootNamespaceName, sourceFileName, host)
{
var baseType = new CodeTypeReference("SimpleTemplateModelBase<dynamic>");
Context.GeneratedClass.BaseTypes.Clear();
Context.GeneratedClass.BaseTypes.Add(baseType);
}
示例5: GenerateCode
private static GeneratorResults GenerateCode(RazorTemplateEntry entry, Type baseType)
{
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = baseType.FullName;
host.DefaultNamespace = "TeamConfig.Razor.Template";
host.DefaultClassName = entry.TemplateName + "Template";
host.NamespaceImports.Add("System");
//filter out page directives and add them as namespace
string templateString = entry.TemplateString;
foreach (Match match in PageDirectivePattern.Matches(templateString))
{
string usedNamespace = match.Groups["Namespace"].Value;
templateString = templateString.Replace(match.Value, string.Empty);
if (usedNamespace.StartsWith("using"))
{
host.NamespaceImports.Add(usedNamespace);
}
}
GeneratorResults razorResult = null;
using (TextReader reader = new StringReader(templateString))
{
var templateEngine = new RazorTemplateEngine(host);
razorResult = templateEngine.GenerateCode(reader);
}
return razorResult;
}
示例6: Engine
public Engine()
{
_host = new RazorEngineHost(new CSharpRazorCodeLanguage());
_host.DefaultBaseClass = "MiniMvc.ViewBase";
//_host.GeneratedClassContext = new System.Web.Razor.Generator.GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo", "WriteLiteralTo", "something", "DefineSection", "BeginContext", "EndContext");
_host.GeneratedClassContext = new System.Web.Razor.Generator.GeneratedClassContext("Execute", "Write", "WriteLiteral", null, null, null, "DefineSection", null, null);
}
示例7: Compile
public Type Compile(string className, CodeCompileUnit codeCompileUnit, RazorEngineHost host)
{
var compilerParameters = new CompilerParameters {GenerateInMemory = true, CompilerOptions = "/optimize"};
AppDomain.CurrentDomain.GetAssemblies()
.Where(x => !x.IsDynamic)
.Each(x => compilerParameters.ReferencedAssemblies.Add(x.Location));
CompilerResults compilerResults;
using (var codeDomProvider = Activator.CreateInstance(host.CodeLanguage.CodeDomProviderType).As<CodeDomProvider>())
{
compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);
if (compilerResults.Errors.HasErrors)
{
using (var sw = new StringWriter())
using (var tw = new IndentedTextWriter(sw, " "))
{
codeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit, tw, new CodeGeneratorOptions());
var source = sw.ToString();
throw CreateExceptionFromCompileError(compilerResults, source);
}
}
}
var templateTypeName = "{0}.{1}".ToFormat(host.DefaultNamespace, className);
var templateType = compilerResults.CompiledAssembly.GetType(templateTypeName);
return templateType;
}
示例8: HtmlMinifierMvcCSharpRazorCodeGenerator
public HtmlMinifierMvcCSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName,
RazorEngineHost host, IHtmlPageMinifier htmlPageMinifier, IDebugStatusReader debugStatusReader)
: base(className, rootNamespaceName, sourceFileName, host)
{
m_HtmPagelMinifier = htmlPageMinifier;
m_DebugStatusReader = debugStatusReader;
}
開發者ID:LaboFoundation,項目名稱:Labo.WebSiteOptimizer,代碼行數:7,代碼來源:HtmlMinifierMvcCSharpRazorCodeGenerator.cs
示例9: MvcCSharpRazorCodeGenerator
public MvcCSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host)
: base(className, rootNamespaceName, sourceFileName, host)
{
// set the default model type to "dynamic" (Dev10 bug 935656)
// don't set it for "special" pages (such as "_viewStart.cshtml")
SetBaseType(DefaultModelTypeName);
}
示例10: ParseToCode
public GeneratorResults ParseToCode(string TemplateCode, string defaultnamespace, string defaultclassname, string baseClassFullName)
{
GeneratorResults razorResults;
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = baseClassFullName;//typeof(BulaqTemplateForRazorBase).FullName;
host.DefaultNamespace = defaultnamespace;
host.DefaultClassName = defaultclassname;
host.NamespaceImports.Add("System");
host.NamespaceImports.Add("BulaqCMS.Models");
host.GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral");
var engine = new RazorTemplateEngine(host);
using (var reader = new StringReader(TemplateCode))
{
razorResults = engine.GenerateCode(reader);
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
using (StringWriter writer = new StringWriter())
{
IndentedTextWriter indentwriter = new IndentedTextWriter(writer, " ");
codeProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, indentwriter, options);
indentwriter.Flush();
indentwriter.Close();
LastGeneratedCode = writer.GetStringBuilder().ToString();
string codePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\code.cs";
File.WriteAllText(codePath, LastGeneratedCode, Encoding.UTF8);
}
}
return razorResults;
}
示例11: ReadBlockSpans
void ReadBlockSpans(string markup)
{
var razorEngineHost = new RazorEngineHost(codeLanguage);
var engine = new RazorTemplateEngine(razorEngineHost);
var results = engine.ParseTemplate(new StringReader(markup));
spans = new List<Span>(results.Document.Flatten());
spans.RemoveAll(span => !span.IsBlock);
}
示例12: ReadHtmlSpans
void ReadHtmlSpans(string html)
{
RazorEngineHost razorEngineHost = new RazorEngineHost(codeLanguage);
RazorTemplateEngine engine = new RazorTemplateEngine(razorEngineHost);
ParserResults results = engine.ParseTemplate(new StringReader(html));
spans = new List<Span>(results.Document.Flatten());
spans.RemoveAll(span => span.Kind != SpanKind.Markup);
}
示例13: SimpleRazorBuildProvider
public SimpleRazorBuildProvider()
{
this._codeLanguage = new CSharpRazorCodeLanguage();
this._compilerType = GetDefaultCompilerTypeForLanguage(this._codeLanguage.LanguageName);
this._host = new SimpleRazorEngineHost(this._codeLanguage);
this._virtualPathDependencies = null;
this._typeName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", this._host.DefaultNamespace, "Foot");
}
示例14: MvcCSharpRazorCodeGenerator
public MvcCSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host)
: base(className, rootNamespaceName, sourceFileName, host) {
var mvcHost = host as MvcWebPageRazorHost;
if (mvcHost != null && !mvcHost.IsSpecialPage) {
// set the default model type to "dynamic" (Dev10 bug 935656)
// don't set it for "special" pages (such as "_viewStart.cshtml")
SetBaseType(_defaultModelTypeName);
}
}
示例15: RazorTemplateEngine
/// <summary>
/// Constructs a new RazorTemplateEngine with the specified host
/// </summary>
/// <param name="host">The host which defines the environment in which the generated template code will live</param>
public RazorTemplateEngine(RazorEngineHost host)
{
if (host == null)
{
throw new ArgumentNullException("host");
}
Host = host;
}