asp.net-mvc-3 – asp.net mvc3返回原始html来查看

还有其他方法可以从控制器返回原始HTML吗?而不是只使用viewbag.如下:
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.HtmlOutput = "<HTML></HTML>";
        return View();
    }
}

@{
    ViewBag.Title = "Index";
}

@Html.Raw(ViewBag.HtmlOutput)

解决方法

这样做没有多大意义,因为View应该生成html,而不是控制器.但无论如何,你可以使用 Controller.Content method,它可以指定结果html,内容类型和编码
public ActionResult Index()
{
    return Content("<html></html>");
}

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

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

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....