asp.net mvc3 return raw html to view

后端 未结 7 678
时光说笑
时光说笑 2020-12-13 08:01

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

public class HomeController : Controller
{
    publ         


        
相关标签:
7条回答
  • 2020-12-13 08:47

    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.

    0 讨论(0)
提交回复
热议问题