In MVC, how do I return a string result?

In my AJAX call, I want to return a string value back to the calling page.

Should I use ActionResult or just return a string?


You can just use the ContentResult to return a plain string:

public ActionResult Temp() {
    return Content("Hi there!");
}

ContentResult by default returns a text/plain as its contentType. This is overloadable so you can also do:

return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");

You can also just return string if you know that's the only thing the method will ever return. For example:

public string MyActionName() {
  return "Hi there!";
}

public ActionResult GetAjaxValue()
{
   return Content("string value");
}
链接地址: http://www.djcxy.com/p/42114.html

上一篇: 如何将存储在字符串变量中的HTML代码传递给MVC 5中的视图?

下一篇: 在MVC中,我如何返回字符串结果?