简体   繁体   English

在没有网站项目的情况下使用ASP.NET用户控件进行模板制作?

[英]Using ASP.NET usercontrols for templating without having a Website project?

Is there a way of using ASP.NET user-controls (.ascx files) to fill my own templating needs within a .NET application, without that application being a Website? 有没有一种方法可以使用ASP.NET用户控件(.ascx文件)来满足.NET应用程序中我自己的模板需求,而该应用程序不是网站?

It's actually a class library that will take data in, combine it with a template, then spit it out. 实际上,这是一个类库,它将接收数据,将其与模板组合,然后将其吐出。

I don't want to write my own templating system from scratch, so I'm looking to re-use ASP.NET functionality for my needs. 我不想从头开始编写自己的模板系统,因此我希望重新使用ASP.NET功能来满足我的需求。

I was told that ASP.NET usercontrol rendering is reliant on IIS. 有人告诉我ASP.NET用户控件呈现依赖于IIS。 Is there some hack or trick to make it work in a .NET class library? 是否有一些技巧可以使它在.NET类库中工作?

I'm not totally sure if I'm following the question properly, but if you are just looking for a templating engine you might want to check out NVelocity , which is part of the Castle Project. 我不确定我是否正确地遵循了这个问题,但是如果您只是在寻找一个模板引擎,您可能想看看NVelocity ,它是Castle Project的一部分。

It allows you to create a template like this (ripped from their getting started guide on the site): 它允许您创建这样的模板(从网站上的入门指南中删除):

From: $from
To: $to
Subject: $subject

Hello $customer.Name

We're please to yada yada yada.

And then propulate it like so: 然后像这样进行传播:

Template template = velocity.GetTemplate(@"path/to/myfirsttemplate.vm");
VelocityContext context = new VelocityContext();
context.Put("from", "somewhere");
context.Put("to", "someone");
context.Put("subject", "Welcome to NVelocity");
context.Put("customer", new Customer("John Doe") );

StringWriter writer = new StringWriter();
template.Merge(context, writer);
Console.WriteLine(writer.GetStringBuilder().ToString());

In addition to Rajsh's answer I'd like to point out that there is a web deployment project template for Visual Studio, which allows you to precompile .ascx files into a single assembly. 除了Rajsh的回答,我还要指出,还有一个Visual Studio的Web部署项目模板 ,它允许您将.ascx文件预编译为单个程序集。

It simplifies your deployment/installation process. 它简化了您的部署/安装过程。 You don't have to copy/synchronize new ascx files, just the created DLL. 您不必复制/同步新的ascx文件,只需复制创建的DLL。

Here is a link to the MSDN article . 这是MSDN文章的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM