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

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

问题描述

还有没有其他的方式,我可以从控制器返回原始HTML?而不是仅仅使用viewbag。象下面这样:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        ViewBag.HtmlOutput =< HTML>< / HTML>中;
        返回查看();
    }
}@ {
    ViewBag.Title =指数;
}@ Html.Raw(ViewBag.HtmlOutput)


解决方案

有在做,没有多大意义,因为查看应生成HTML,而不是控制。但不管怎么说,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.content.aspx\">Controller.Content方法,它给你指定的结果HTML的能力,也是内容类型和编码

 公众的ActionResult指数()
{
    返回内容(&LT; HTML和GT;&LT; / HTML&gt;中);
}

或者你可以使用内置的asp.net-MVC框架的伎俩 - 直接使动作返回的字符串。这将提供字符串的内容到用户的浏览器。

 公共字符串指数()
{
    回归&LT; HTML和GT;&LT; / HTML&gt;中;
}

在事实上,超过的ActionResult 之外的任何作用的结果,框架,尝试将其序列化到字符串写入响应。

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)

解决方案

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>");
}

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>";
}

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天全站免登陆