0

我必须在 ASP.NET MVC 视图的 IFrame 中呈现 html。我将视图中 iframe 的“源”设置为“控制器操作”。但是,我没有看到嵌入在我的视图中的实际 html,而是看到了 html 源代码和 html 文本中的一些特殊字符。我在这里有一个相同的屏幕截图:https: //plus.google.com/photos/117026675016318325824/albums/5797507243229043425?banner=pwa&gpsrc=pwrd1#photos /117026675016318325824/albums/5797507243229042wrdbannerp2 还想要&gpsrc=wrdbanner2补充一点,一个 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();
    }
}
4

1 回答 1

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();
        }
    }
}
于 2012-10-10T04:20:32.607 回答