asp.net mvc3 返回原始 html 以查看 [英] asp.net mvc3 return raw html to view

查看:23
本文介绍了asp.net mvc3 返回原始 html 以查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有其他方法可以从控制器返回原始 html 吗?与仅使用 viewbag 不同.如下图:

Are there other ways I can return raw html from controller? As opposed to just using viewbag. like below:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.HtmlOutput = "<HTML></HTML>";
        return View();
    }
}

@{
    ViewBag.Title = "Index";
}

@Html.Raw(ViewBag.HtmlOutput)

推荐答案

这样做没有多大意义,因为 View 应该生成 html,而不是控制器.但无论如何,您可以使用 Controller.Content 方法,它使您能够指定结果 html,以及内容类型和编码

There's no much point in doing that, because View should be generating html, not the controller. But anyways, you could use Controller.Content method, which gives you ability to specify result html, also content-type and encoding

public ActionResult Index()
{
    return Content("<html></html>");
}

或者你可以使用asp.net-mvc框架中内置的技巧——直接让动作返回字符串.它将字符串内容传送到用户的浏览器中.

Or you could use the trick built in asp.net-mvc framework - make the action return string directly. It will deliver string contents into users's browser.

public string Index()
{
    return "<html></html>";
}

实际上,对于ActionResult以外的任何动作结果,框架都会尝试将其序列化为字符串并写入响应.

In fact, for any action result other than ActionResult, framework tries to serialize it into string and write to response.

这篇关于asp.net mvc3 返回原始 html 以查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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