开发者

Is it possible to return anything else from controller apart from view?

开发者 https://www.devze.com 2022-12-29 22:46 出处:网络
Is it possible to return a string from controller upon a form s开发者_运维问答ubmission ?[HttpPost]

Is it possible to return a string from controller upon a form s开发者_运维问答ubmission ?


[HttpPost]
public ActionResult Index()
{
    return Content("some string", "text/plain");
}


You can, as Darin suggest, return Content(string); There are also other possibilities such as

[HttpPost]
public ActionResult Index(FormCollection form) {
    /*
      return Json(json);
      return View;
      return PartialView;
    */
}

If you return something other than an action result it will automatically be wrapped in a ContentResult.

[HttpPost]
public ContentResult Index(FormCollection form) {
    /*
      return Content(string); 
      return File(bytes, contentType);
      return DateTime.Now;
      return 2;
    */
}


public ActionResult Index()
{
    // ...
    return File(bytes, contentType);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消