697

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?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
user67033
  • 15,945
  • 6
  • 21
  • 11

7 Answers7

1176

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");
Bakudan
  • 19,134
  • 9
  • 53
  • 73
swilliams
  • 48,060
  • 27
  • 100
  • 130
  • What is the contentType if your return type is a string? – user1886419 Jan 29 '14 at 15:16
  • 7
    I don't know how accurate this answer was back then, but currently `ContentResult` does `if (!String.IsNullOrEmpty(ContentType))` before setting `HttpContext.Response.ContentType`. I'm seeing `text/html` with your first example, either that's the default now or it's an educated guess by the `HttpContext`. – user247702 Mar 12 '14 at 10:10
  • How can I Access in View ? – Pradeep Kumar Das Mar 22 '17 at 12:55
  • 5
    Small addition: instead of literally adding "text/plain" as a string, you could use a .NET framework constant like `MediaTypeNames.Text.Plain` or `MediaTypeNames.Text.Xml`. Although it only includes some of the most-used MIME types. _( https://learn.microsoft.com/en-us/dotnet/api/system.net.mime.mediatypenames )_ – Doku-so Nov 15 '17 at 12:45
  • Up voted, though I did need to specify the mime type as "text/plain" when returning HTML as text per @Stijn comment. – Yogi Jan 24 '18 at 23:46
117

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!";
}
Haacked
  • 58,045
  • 14
  • 90
  • 114
  • 10
    Phil, is this a "Best Practice", could you please explain the difference between the your answer and @swilliam 's – David Perlman Nov 11 '12 at 09:59
  • 10
    You can't return a string from a method which returns an ActionResult, so in this case you return Content("") as swilliams explained. If you only ever need to return a string, then you would have the method return a string, as Phil explained. – Arkiliknam Jan 25 '13 at 15:17
  • 3
    Assuming that same action has multiple `return` statements which are used to send either `string` or `JSON` or `View` based on conditions then we must use `Content` to return string. – DhruvJoshi Aug 26 '15 at 12:53
  • What is the trick for the compiler to allow return type to be "string" if expecting IActionResult? – Pete Alvin Jan 30 '23 at 18:12
21
public ActionResult GetAjaxValue()
{
   return Content("string value");
}
6

As of 2020, using ContentResult is still the right approach as proposed above, but the usage is as follows:

return new System.Web.Mvc.ContentResult
{
    Content = "Hi there! ☺",
    ContentType = "text/plain; charset=utf-8"
}
Jack Miller
  • 6,843
  • 3
  • 48
  • 66
3

There Are 2 ways to return a string from the controller to the view:

First

You could return only the string, but it will not be included in your .cshtml file. it will be just a string appearing in your browser.


Second

You could return a string as the Model object of View Result.

Here is the code sample to do this:

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

    }
}

In the view file to run AutoProperty, It will redirect you to the Result view and will send s
code to the view

<!--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>

I run this at http://localhost:60227/Home/AutoProperty.

Benj Sanders
  • 481
  • 5
  • 15
ahmed khattab
  • 2,311
  • 3
  • 16
  • 30
0
public JsonResult GetAjaxValue() 
{
  return Json("string value", JsonRequetBehaviour.Allowget); 
}
Naktibalda
  • 13,705
  • 5
  • 35
  • 51
Khursheed
  • 343
  • 4
  • 13
0

you can just return a string but some API's do not like it as the response type is not fitting the response,

[Produces("text/plain")]
public string Temp() {
    return Content("Hi there!");
}

this usually does the trick

Walter Verhoeven
  • 3,867
  • 27
  • 36