asp.net mvc 中的 response.write 是什么?

what is response.write in asp.net mvc?

这会很简单但是

在 asp 网络 MVC 中使用经典网络表单 "response.write" 的最佳方式是什么。特别是mvc5.

比方说:我只想从控制器写一个简单的字符串到屏幕。

mvc中是否存在response.write?

谢谢。

如果您的方法的 return 类型是 ActionResult,您可以使用 Content 方法来 return 任何类型的内容。

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

或者干脆

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

Content 方法还允许您 return 其他内容类型,只需将内容类型作为第二个参数传递即可。

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

正如@Shyju 所说,您应该使用 Content 方法,但是还有另一种创建自定义操作结果的方法,您的自定义操作结果可能如下所示::

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

然后就可以这样使用了:

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

        return new MyActionResult("content");
    }