0

with asp.net framework, I have used Response.Write("string here") to display data from controller on console of web browser app. But how do we do that in .net core?

I have tried

HttpResponseWritingExtensions.WriteAsync(Response,"string here");

But it shows a network connection error. Please help me. Thanks

LaMars
  • 45
  • 2
  • 10
  • 2
    You mean the dev tools (F12) console tab? `Response.Write` just writes to the response stream going to the browser (eg the html content), not the console specifically. Am I misunderstanding? The dev tools console is for Javascript messages, eg. `console.log`. – Jim W Mar 26 '18 at 04:38

2 Answers2

4

The easiest way is to use

Console.WriteLine("Any String");
luke77
  • 2,255
  • 2
  • 18
  • 30
Farrukh Ahmed Khan
  • 301
  • 1
  • 3
  • 19
2

FYI Response.Write is removed from MVC.

There are two Solutions for .Net Core MVC is by using Content Result or by writing your custom ActionResult by overriding ExecuteResult method as below.

1. Using Content Result

 public IActionResult Index()
 {

  return Content("Hello World !");
 }

or even Just return string method

public string Index()
{
  return "Hello World !";
}

2. Custom Action Result:

 public class ResponseWriteActionResult : ActionResult
 {
      public override void ExecuteResult(ActionContext context)
      {
          var str = Encoding.ASCII.GetBytes("Hello World !");

          context.HttpContext.Response.Body.Write(str, 0, str.Length);
      }

 }

Usage:

public IActionResult Index()
{

   return new ResponseWriteActionResult();
}

References:

1.

2.

3.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196