在ASP.NET MVC视图中在没有特殊字符(?)的IFrame中呈现html

时间:2012-10-10 03:00:07

标签: c# asp.net-mvc iframe

我必须在ASP.NET MVC视图中的IFrame中渲染html。我在视图中将iframe的“source”设置为“controller action”。但是,我没有看到嵌入在我视图中的实际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();
    }
}

1 个答案:

答案 0 :(得分:0)

您可能需要将视图提取为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();
        }
    }
}