在 ASP .NET MVC 3 中使用 XSLT [英] Using XSLT in ASP .NET MVC 3

查看:26
本文介绍了在 ASP .NET MVC 3 中使用 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有在 asp.net MVC 3 中使用 XSLT 的经验?

这里的目的是能够开发可以根据某些条件在运行时更改样式和布局的页面.例如,用户个人资料.

一种解决方案是,我们可以使用单独的布局页面,并在运行时通过设置动态属性 Viewbag 进行设置.但是如果我们想向页面添加新布局,这种方法将需要重新编译.我在想,也许我们可以在运行时在控制器中动态加载 XSL,并在运行时将其绑定到模型对象.然后可以在页面的预定义部分中呈现 HTML 内容.

代码片段会很有帮助.

解决方案

我刚刚构建了一个将 XML 转换为 HTML 以在 MVC3 中显示的站点.我使用了第二种技术,其中控制器确定要使用的 XML 和 XSLT 文件,并将它们传递到模型中.视图中的 HTML 帮助程序实际执行转换.

在本例中,我正在渲染一个会议程序,这就是下面的 Program 所指的内容.可以向样式表提供参数;在下面,我提供了一个基本 URL 作为参数,它将在生成的 HTML 中转换为链接.

模型:

公共类 ProgramModel{公共字符串 ProgramFilename { 获取;放;}公共字符串样式表文件名 { 获取;放;}公共字典<字符串,字符串>参数 { 获取;保护集;}公共程序模型(){参数 = new Dictionary();}}

控制器:

 [OutputCache(Duration=1000)]公共 ActionResult 索引(){string xmlFile = Server.MapPath("~/Program.xml");string xsltFile = Server.MapPath("~/Program-index.xslt");Response.AddCacheDependency(new CacheDependency(xmlFile), new CacheDependency(xsltFile));ProgramModel 模型 = new ProgramModel();模型.ProgramFilename = xmlFile;model.StylesheetFilename = xsltFile;model.Parameters["baseDayUrl"] = Url.Action("Day");返回视图(模型);}

帮手:

公共静态类 HtmlHelperXmlExtensions{///<总结>///将 XSL 转换应用于 XML 文档.///</总结>public static HtmlString RenderXml(this HtmlHelper helper, string xmlPath, string xsltPath, IDictionary 参数){XsltArgumentList args = new XsltArgumentList();如果(参数!= null)foreach(参数中的字符串键.Keys)args.AddParam(key, "", parameters[key]);XslCompiledTransform t = new XslCompiledTransform();t.Load(xsltPath);XmlReaderSettings settings = new XmlReaderSettings();settings.DtdProcessing = DtdProcessing.Parse;settings.ValidationType = ValidationType.DTD;使用 (XmlReader reader = XmlReader.Create(xmlPath, settings)){StringWriter writer = new StringWriter();t.Transform(reader, args, writer);返回新的 HtmlString(writer.ToString());}}}

视图:

@Html.RenderXml(Model.ProgramFilename, Model.StylesheetFilename, Model.Parameters)

Does anybody has experience in in using XSLT in asp.net MVC 3?

The intention here is to be able to develop pages whose styling and layout can be changed at runtime based on some conditions. for example, user profile.

One solution is that We can use separate layout pages and set that at runtime by setting the dynamic property Viewbag. But this approach would require a recompile if we want to add new layout to the page. I was thinking that may be we could load an XSL dynamically in the controller at runtime and bind it to the model object at runtime. The HTML content can then be rendered in a predefined section in the page.

A code snippet would be a great help.

解决方案

I just built a site that transforms XML into HTML for display in MVC3. I used the second technique, where the controller determines the XML and XSLT files to use, and passes them in the model. An HTML helper in the view actually performs the transform.

In this case I'm rendering a conference program, so that's what Program refers to below. Parameters can be supplied to the stylesheet; below, I'm supplying a base URL as a parameter that will be turned into links in the generated HTML.

The model:

public class ProgramModel
{
    public string ProgramFilename { get; set; }
    public string StylesheetFilename { get; set; }

    public Dictionary<string, string> Parameters { get; protected set; }

    public ProgramModel()
    {
        Parameters = new Dictionary<string, string>();
    }
}

The controller:

    [OutputCache(Duration=1000)]
    public ActionResult Index()
    {
        string xmlFile = Server.MapPath("~/Program.xml");
        string xsltFile = Server.MapPath("~/Program-index.xslt");
        Response.AddCacheDependency(new CacheDependency(xmlFile), new CacheDependency(xsltFile));

        ProgramModel model = new ProgramModel();
        model.ProgramFilename = xmlFile;
        model.StylesheetFilename = xsltFile;
        model.Parameters["baseDayUrl"] = Url.Action("Day");

        return View(model);
    }

The helper:

public static class HtmlHelperXmlExtensions
{
    /// <summary>
    /// Applies an XSL transformation to an XML document.
    /// </summary>
    public static HtmlString RenderXml(this HtmlHelper helper, string xmlPath, string xsltPath, IDictionary<string,string> parameters)
    {
        XsltArgumentList args = new XsltArgumentList();
        if (parameters != null)
            foreach (string key in parameters.Keys)
                args.AddParam(key, "", parameters[key]);

        XslCompiledTransform t = new XslCompiledTransform();
        t.Load(xsltPath);

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Parse;
        settings.ValidationType = ValidationType.DTD;

        using (XmlReader reader = XmlReader.Create(xmlPath, settings))
        {
            StringWriter writer = new StringWriter();
            t.Transform(reader, args, writer);
            return new HtmlString(writer.ToString());
        }

    }

}

The view:

<div data-role="content">
@Html.RenderXml(Model.ProgramFilename, Model.StylesheetFilename, Model.Parameters)
</div>

这篇关于在 ASP .NET MVC 3 中使用 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆