包装/修改HTML结果后 [英] Wrapping/Modifying Html Result

查看:80
本文介绍了包装/修改HTML结果后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我们正处在一个重要的哈克情况。我们有链接到从其他网站几个网页。然而,要求是,该站点具有布局链接到我们站点相同。这最初是由请求原来的页面,刮布局,并在他们的布局包裹出的内容做了。

Basically we are in a major hacky situation. We have a few web pages that is linked to from an other site. However, the requirement is that this site has the same layout as the site that linked to us. This was originally done by requesting the original page, scraping the layout, and wrapping out content in their layout.

这是Web窗体相当简单,因为我们可以简单地创建一个子类页,重写Render方法再包东西我们在外部网站布局生产。但是,现在这个项目正在重写的ASP.NET MVC。

This was rather simple in Web Forms as we could simply create a subclassed Page, that overrides the Render method and then wrapped anything we produced in the external sites layout. However, now this project is being rewritten in ASP.NET MVC.

我们怎样才能获得acccess由MVC行动创建的HTML结果,根据我们的需要和输出修改后的结果给浏览器修改它们?

How can we get acccess to the HTML result created by the MVC actions, modify them according to our needs and output the modified result to the browser?

推荐答案

尝试过tugberk的解决方案,这是不够的我结束了创建自定义视图结果后:

After having tried tugberk's solution which was insufficient I ended up creating a custom view result:

 public class WrappedViewResult : ViewResult
    {
        private readonly object model;
        public WrapInDtuViewResult()
        {

        }

        public WrapInDtuViewResult(object model)
        {
            this.model = model;
        }


        public override void ExecuteResult(ControllerContext context)
        {

            if (string.IsNullOrWhiteSpace(this.ViewName))
            {
                this.ViewName = context.RouteData.Values["action"].ToString();
            }
            ViewEngineResult result = this.FindView(context);

            context.Controller.ViewData.Model = model;
            ViewDataDictionary viewData = context.Controller.ViewData;
            TempDataDictionary tempData = context.Controller.TempData;

            var writer = new StringWriter();
            ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, writer);
            result.View.Render(viewContext, writer);


            var content = writer.ToString();


            Scraping scraping = new Scraping();
            if (AppSettings.UseScraping)
            {
                content = scraping.Render(content);
            }
            else
            {
                content = "<html><head><script src='/Scripts/jquery-1.7.1.min.js' type='text/javascript'></script></head><body>" + content + "</body></html>";
            }

            context.HttpContext.Response.Write(content);
        }
    }

这篇关于包装/修改HTML结果后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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