asp.net mvc3: How to return raw html to view?

asp.net Mvc-3ControllerReturn

asp.net Mvc-3 Problem Overview


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)

asp.net Mvc-3 Solutions


Solution 1 - asp.net Mvc-3

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][1], 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. [1]: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.content.aspx

Solution 2 - asp.net Mvc-3

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

Solution 3 - asp.net Mvc-3

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'>
&times;</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.

Solution 4 - asp.net Mvc-3

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

Solution 5 - asp.net Mvc-3

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)

Solution 6 - asp.net Mvc-3

public ActionResult Questionnaire()
{
    return Redirect("~/MedicalHistory.html");
}

Solution 7 - asp.net Mvc-3

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRiverView Question on Stackoverflow
Solution 1 - asp.net Mvc-3archilView Answer on Stackoverflow
Solution 2 - asp.net Mvc-3Adam TuliperView Answer on Stackoverflow
Solution 3 - asp.net Mvc-3Shaiju TView Answer on Stackoverflow
Solution 4 - asp.net Mvc-3MattView Answer on Stackoverflow
Solution 5 - asp.net Mvc-3MoXplodView Answer on Stackoverflow
Solution 6 - asp.net Mvc-3saktiprasad swainView Answer on Stackoverflow
Solution 7 - asp.net Mvc-3waris kantrooView Answer on Stackoverflow