【问题标题】:In MVC, how do I return a string result?在 MVC 中,如何返回字符串结果?
【发布时间】:2010-10-07 22:11:21
【问题描述】:

在我的 AJAX 调用中,我想将一个字符串值返回给调用页面。

我应该使用ActionResult 还是只返回一个字符串?

【问题讨论】:

  • 检查 here返回引导警报消息

标签: asp.net-mvc ajax actionresult


【解决方案1】:

您可以只使用ContentResult 来返回一个纯字符串:

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

ContentResult 默认返回一个text/plain 作为它的contentType。这是可重载的,所以你也可以这样做:

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

【讨论】:

  • 如果您的返回类型是字符串,那么 contentType 是什么?
  • 我不知道当时这个答案有多准确,但目前ContentResult 在设置HttpContext.Response.ContentType 之前会执行if (!String.IsNullOrEmpty(ContentType))。我在您的第一个示例中看到text/html,要么这是现在的默认设置,要么是HttpContext 的有根据的猜测。
  • 如何在视图中访问?
  • 小添加:您可以使用 .NET 框架常量,例如 MediaTypeNames.Text.PlainMediaTypeNames.Text.Xml,而不是直接添加“text/plain”作为字符串。虽然它只包括一些最常用的 MIME 类型。 (docs.microsoft.com/en-us/dotnet/api/…)
  • 投了赞成票,尽管在根据@Stijn 评论将 HTML 作为文本返回时,我确实需要将 mime 类型指定为“text/plain”。
【解决方案2】:

如果您知道这是该方法唯一会返回的内容,您也可以只返回字符串。例如:

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

【讨论】:

  • Phil,这是“最佳实践”吗,您能否解释一下您的答案与@swilliam 的答案之间的区别
  • 您不能从返回 ActionResult 的方法中返回字符串,因此在这种情况下,您会按照 swilliams 的解释返回 Content("")。如果您只需要返回一个字符串,那么您可以让该方法返回一个字符串,正如 Phil 解释的那样。
  • 假设同一个动作有多个return语句用于根据条件发送stringJSONView,那么我们必须使用Content返回字符串。
【解决方案3】:
public ActionResult GetAjaxValue()
{
   return Content("string value");
}

【讨论】:

  • 回答时最好多解释
【解决方案4】:

截至2020年,使用ContentResult仍然是建议above的正确方法,但用法如下:

return new System.Web.Mvc.ContentResult
{
    Content = "Hi there! ☺",
    ContentType = "text/plain; charset=utf-8"
}

【讨论】:

    【解决方案5】:

    有两种方法可以将字符串从控制器返回到视图:

    第一

    您可以只返回字符串,但它不会包含在您的 .cshtml 文件中。它只是出现在您的浏览器中的一个字符串。


    第二

    您可以返回一个字符串作为 View Result 的 Model 对象。

    这是执行此操作的代码示例:

    public class HomeController : Controller
    {
        // GET: Home
        // this will return just a string, not html
        public string index()
        {
            return "URL to show";
        }
    
        public ViewResult AutoProperty()
        {   
            string s = "this is a string ";
            // name of view , object you will pass
            return View("Result", s);
    
        }
    }
    

    在视图文件中运行AutoProperty,它会将你重定向到Result视图并发送s
    代码到视图

    <!--this will make this file accept string as it's model-->
    @model string
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Result</title>
    </head>
    <body>
        <!--this will represent the string -->
        @Model
    </body>
    </html>
    

    我在 http://localhost:60227/Home/AutoProperty 运行它。

    【讨论】:

      【解决方案6】:
      public JsonResult GetAjaxValue() 
      {
        return Json("string value", JsonRequetBehaviour.Allowget); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-18
        • 1970-01-01
        • 2015-01-05
        相关资源
        最近更新 更多