[英]how to print text on web console with asp.net core c#
使用 asp.net 框架,我使用 Response.Write("string here") 在 Web 瀏覽器應用程序的控制台上顯示來自控制器的數據。 但是我們如何在 .net core 中做到這一點?
我試過了
HttpResponseWritingExtensions.WriteAsync(Response,"string here");
但它顯示網絡連接錯誤。 請幫幫我。 謝謝
最簡單的方法是使用
Console.WriteLine("Any String");
僅供參考Response.Write
從MVC
刪除。
.Net Core MVC
有兩種解決方案,一種是使用Content Result
, ExecuteResult
一種是通過重寫ExecuteResult
方法編寫自定義ActionResult
,如下所示。
1. 使用內容結果
public IActionResult Index()
{
return Content("Hello World !");
}
甚至只是返回string
方法
public string Index()
{
return "Hello World !";
}
2. 自定義操作結果:
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);
}
}
用法:
public IActionResult Index()
{
return new ResponseWriteActionResult();
}
參考資料:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.