【问题标题】:Render html in IFrame without Special Characters(?) in ASP.NET MVC view在 ASP.NET MVC 视图中在没有特殊字符(?)的 IFrame 中呈现 html
【发布时间】:2012-10-10 03:00:07
【问题描述】:

我必须在 ASP.NET MVC 视图的 IFrame 中呈现 html。我将视图中 iframe 的“源”设置为“控制器操作”。但是,我没有看到嵌入在我的视图中的实际 html,而是看到了 html 源代码和 html 文本中的一些特殊字符。我在这里有一个相同的屏幕截图: https://plus.google.com/photos/117026675016318325824/albums/5797507243229043425?banner=pwa&gpsrc=pwrd1#photos/117026675016318325824/albums/5797507243229043425?banner=pwa&gpsrc=pwrd1 我还想补充一点,一个 pdf 文档和一个文本文档使用相同的代码可以很好地呈现,只有呈现 html 文档是一个问题。

如何让视图以正确的格式显示 html?

控制器:

public BinaryNonBinaryActionResult GetAgreement(string id)
{
    string agreementText = "<FONT color=deepskyblue><STRONG> test text</STRONG></FONT>";
    return new BinaryNonBinaryActionResult(GetBytes(agreementText), "text/html");
}
 static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

public class BinaryNonBinaryActionResult : ActionResult
{
    private byte[] bytes;
    private string contentType ;
    public BinaryNonBinaryActionResult(byte[] bytes, string contentType)
    {
        this.bytes =bytes;
        this.contentType = contentType;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        var response = context.HttpContext.Response;
        response.ContentType = contentType;
        var imageStream = new MemoryStream(bytes);
        var buffer = new byte[4096];
        while (true)
        {
            var read = imageStream.Read(buffer, 0, buffer.Length);
            if (read == 0)
                break;
            response.OutputStream.Write(buffer, 0, read);
        }
        response.End();
    }
}

【问题讨论】:

  • 我通过将代码更改为来解决此问题: public BinaryNonBinaryActionResult GetAgreement(string id) { string agreementText = " test text"; return new BinaryNonBinaryActionResult(Encoding.ASCII.GetBytes(agreementText), "text/html"); }。感谢@Sathish 指出这一点。

标签: c# asp.net-mvc iframe


【解决方案1】:

您可能需要将视图提取为 HTML 字符串。请试试这个。

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return View();
    }

    public ActionResult About()
    {
        return View();
    }

    public BinaryNonBinaryActionResult GetAgreement(string id) 
    { 
        string agreementText = "<FONT color=deepskyblue><STRONG> test text</STRONG></FONT>"; 
        return new BinaryNonBinaryActionResult(GetBytes(agreementText), "text/html"); 
    }

    public byte[] GetBytes(string input)
    {

        string myString = this.RenderViewToString("About", this.ViewData);
        return Encoding.ASCII.GetBytes(myString);
    }


}

public static class RenderExtended
{
    public static string RenderViewToString(this Controller controller, string viewName, object viewData)
    {
        //Create memory writer     
        var sb = new StringBuilder();
        var memWriter = new StringWriter(sb);
        //Create fake http context to render the view     
        var fakeResponse = new HttpResponse(memWriter);
        var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
        var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), controller.ControllerContext.RouteData, controller.ControllerContext.Controller);
        var oldContext = HttpContext.Current;
        HttpContext.Current = fakeContext;

        //Use HtmlHelper to render partial view to fake context     
        var html = new HtmlHelper(new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), memWriter), new ViewPage());
        html.RenderPartial(viewName, viewData);
        //Restore context     
        HttpContext.Current = oldContext;
        //Flush memory and return output     
        memWriter.Flush();
        return sb.ToString();
    }
    public class FakeView : IView
    {
        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            throw new NotImplementedException();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-01-19
    • 2011-01-28
    • 2013-03-11
    • 1970-01-01
    • 2021-06-16
    • 2016-07-11
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多