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

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?

转载于:https://stackoverflow.com/questions/553936/in-mvc-how-do-i-return-a-string-result

weixin_41568183
零零乙 check here to return bootstrap alert message
大约 5 年之前 回复

4个回答

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");
csdnceshi52
妄徒之命 Up voted, though I did need to specify the mime type as "text/plain" when returning HTML as text per @Stijn comment.
2 年多之前 回复
weixin_41568174
from.. 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. ( docs.microsoft.com/en-us/dotnet/api/… )
2 年多之前 回复
csdnceshi70
笑故挽风 How can I Access in View ?
3 年多之前 回复
csdnceshi75
衫裤跑路 Nice response..
接近 6 年之前 回复
weixin_41568184
叼花硬汉 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.
6 年多之前 回复
weixin_41568196
撒拉嘿哟木头 Or, we could hit the down arrow, to get it back down to 265! ha! (j/k, up'ped it)
6 年多之前 回复
csdnceshi61
derek5. What is the contentType if your return type is a string?
6 年多之前 回复
csdnceshi79
python小菜 Well, Nordvind, you just have to work to get it up to 512 I suppose. :)
大约 7 年之前 回复
csdnceshi59
ℙℕℤℝ Everyday you learn something new!
大约 8 年之前 回复
weixin_41568127
?yb? That is nice! I also didn't know about this.
接近 10 年之前 回复
csdnceshi62
csdnceshi62 Ah thanks, never knew about this :).
11 年多之前 回复

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!";
}
csdnceshi56
lrony* 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.
接近 5 年之前 回复
weixin_41568127
?yb? 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.
7 年多之前 回复
csdnceshi65
larry*wei Phil, is this a "Best Practice", could you please explain the difference between the your answer and @swilliam 's
7 年多之前 回复
public JsonResult GetAjaxValue() 
{
  return Json("string value", JsonRequetBehaviour.Allowget); 
}

there is 2 way to return a string from controller to the view

first

you could return only string but will not be included in html file it will be jus string appear in browser


second

could return a string as object of View Result

here is the code samples to do this

public class HomeController : Controller
{
    // GET: Home
    // this will mreturn just 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", (object)s);

    }
}

in view file to run AutoProperty it will redirect you to Result view and will send s
code to view

<!--this to make this file accept string as model-->
@model string

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Result</title>
</head>
<body>
    <!--this is for represent the string -->
    @Model
</body>
</html>

i run it at http://localhost:60227/Home/AutoProperty

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问