what is response.write in asp.net mvc?

◇◆丶佛笑我妖孽 提交于 2019-12-04 12:28:25

If the return type of your method is an ActionResult, You can use the Content method to return any type of content.

public ActionResult MyCustomString()
{
   return Content("YourStringHere");
}

or simply

public String MyCustomString()
{
  return "YourStringHere";
}

Content method allows you return other content type as well, Just pass the content type as second param.

 return Content("<root>Item</root>","application/xml");

As @Shyju said you should use Content method, But there's another way by creating a custom action result, Your custom action-result could look like this::

public class MyActionResult : ActionResult
{
    private readonly string _content;

    public MyActionResult(string content)
    {
        _content = content;
    }
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.Write(_content);
    }
}

Then you can use it, this way:

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return new MyActionResult("content");
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!