asp.net mvc3 return raw html to view

后端 未结 7 706
时光说笑
时光说笑 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:33

    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:

    /// 
    /// Startup message displayed in browser.
    /// 
    /// HTML result
    [HttpGet]
    public ContentResult Get()
    {
        var result = Content("DEMO

    Demo started successfully." + "
    Use Swagger" + " to view API.

    "); result.ContentType = "text/html; charset=UTF-8"; return result; }

提交回复
热议问题