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

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

问题描述

有谁在asp.net中使用XSLT MVC 3中的经验吗?

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.

解决方案之一是,我们可以使用不同的页面布局,并通过设置动态属性Viewbag设置在运行时。但这种方法需要重新编译如果我们想新的布局添加到页面。我想这可能是我们可以在运行时在控制器动态加载一个XSL,它在运行时绑定到模型对象。 HTML内容就可以在一个页predefined部分被渲染。

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.

一个code段将是一个很大的帮助。

A code snippet would be a great help.

推荐答案

我刚刚建立了XML转换成HTML在MVC3显示一个站点。我使用的第二技术,其中控制器确定XML和XSLT文件来使用,并将它们在模型中。在查看HTML帮助实际执行变换。

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.

在这种情况下,我呈​​现的会议程序,所以这就是程序指的下方。参数可被提供给样式表;下面,我提供一个基本URL作为将要变成生成的HTML链接的参数。

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.

模型:

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>();
    }
}

控制器:

    [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);
    }

助手:

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());
        }

    }

}

视图:

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

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

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