Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is response.write in asp.net mvc?

This will be quite simple but

What is the best way of using classical webforms "response.write" in asp net MVC. Especially mvc5.

Let's say: I just would like to write a simple string to screen from controller.

Does response.write exist in mvc?

Thanks.

like image 753
oneNiceFriend Avatar asked Dec 08 '22 00:12

oneNiceFriend


2 Answers

If the return type of your method is an ActionResult, You can use the Content method to return any type of content.

public ActionResult MyCustomString()
{
   return Content("YourStringHere");
}

or simply

public String MyCustomString()
{
  return "YourStringHere";
}

Content method allows you return other content type as well, Just pass the content type as second param.

 return Content("<root>Item</root>","application/xml");
like image 136
Shyju Avatar answered Dec 22 '22 23:12

Shyju


As @Shyju said you should use Content method, But there's another way by creating a custom action result, Your custom action-result could look like this::

public class MyActionResult : ActionResult
{
    private readonly string _content;

    public MyActionResult(string content)
    {
        _content = content;
    }
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.Write(_content);
    }
}

Then you can use it, this way:

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return new MyActionResult("content");
    }
like image 28
Sirwan Afifi Avatar answered Dec 23 '22 00:12

Sirwan Afifi