asp.net mvc3:如何返回原始html来查看?

发布于 2024-12-08 09:02:37 字数 315 浏览 1 评论 0原文

还有其他方法可以从控制器返回原始 html 吗?而不是仅仅使用 viewbag。像下面这样:

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

@{
    ViewBag.Title = "Index";
}

@Html.Raw(ViewBag.HtmlOutput)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

2024-12-15 09:02:37

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

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

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

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

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

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.

栖竹 2024-12-15 09:02:37

只需在视图模型中创建 MvcHtmlString 类型的属性即可。那么你也不需要 Html.Raw 。

Simply create a property in your view model of type MvcHtmlString. You won't need to Html.Raw it then either.

跨年 2024-12-15 09:02:37

尝试返回引导警报消息,这对我有用

return Content("<div class='alert alert-success'><a class='close' data-dismiss='alert'>
×</a><strong style='width:12px'>Thanks!</strong> updated successfully</div>");

注意:不要忘记添加引导cssjs< /code> 在您的查看页面

希望对某人有所帮助。

Give a try to return bootstrap alert message, this worked for me

return Content("<div class='alert alert-success'><a class='close' data-dismiss='alert'>
×</a><strong style='width:12px'>Thanks!</strong> updated successfully</div>");

Note: Don't forget to add bootstrap css and js in your view page

hope helps someone.

夏九 2024-12-15 09:02:37

对我(ASP.NET Core)有用的是设置返回类型 ContentResult,然后将 HMTL 包装到其中并将 ContentType 设置为 "text/html; charset=UTF-8 “。这很重要,因为否则它不会被解释为 HTML,并且 HTML 语言将显示为文本。

这是示例,是控制器类的一部分:

/// <summary>
/// Startup message displayed in browser.
/// </summary>
/// <returns>HTML result</returns>
[HttpGet]
public ContentResult Get()
{
    var result = Content("<html><title>DEMO</title><head><h2>Demo started successfully."
      + "<br/>Use <b><a href=\"http://localhost:5000/swagger\">Swagger</a></b>"
      + " to view API.</h2></head><body/></html>");
    result.ContentType = "text/html; charset=UTF-8";
    return result;
}

What was working for me (ASP.NET Core), was to set return type ContentResult, then wrap the HMTL into it and set the ContentType to "text/html; charset=UTF-8". That is important, because, otherwise it will not be interpreted as HTML and the HTML language would be displayed as text.

Here's the example, part of a Controller class:

/// <summary>
/// Startup message displayed in browser.
/// </summary>
/// <returns>HTML result</returns>
[HttpGet]
public ContentResult Get()
{
    var result = Content("<html><title>DEMO</title><head><h2>Demo started successfully."
      + "<br/>Use <b><a href=\"http://localhost:5000/swagger\">Swagger</a></b>"
      + " to view API.</h2></head><body/></html>");
    result.ContentType = "text/html; charset=UTF-8";
    return result;
}
您的好友蓝忘机已上羡 2024-12-15 09:02:37

看起来不错,除非您想将其作为模型字符串传递

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

@model string
@{
    ViewBag.Title = "Index";
}

@Html.Raw(Model)

That looks fine, unless you want to pass it as Model string

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

@model string
@{
    ViewBag.Title = "Index";
}

@Html.Raw(Model)
牵强ㄟ 2024-12-15 09:02:37

在控制器中,您可以使用 MvcHtmlString

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string rawHtml = "<HTML></HTML>";
        ViewBag.EncodedHtml = MvcHtmlString.Create(rawHtml);
        return View();
    }
}

在视图中,您可以简单地使用在控制器中设置的动态属性,如下所示

<div>
        @ViewBag.EncodedHtml
</div>

In controller you can use MvcHtmlString

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string rawHtml = "<HTML></HTML>";
        ViewBag.EncodedHtml = MvcHtmlString.Create(rawHtml);
        return View();
    }
}

In your View you can simply use that dynamic property which you set in your Controller like below

<div>
        @ViewBag.EncodedHtml
</div>
拔了角的鹿 2024-12-15 09:02:37
public ActionResult Questionnaire()
{
    return Redirect("~/MedicalHistory.html");
}
public ActionResult Questionnaire()
{
    return Redirect("~/MedicalHistory.html");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文