简体   繁体   English

HttpContext.Response.WriteAsync("<h1> 你好世界</h1> ") onGet 处理程序方法在 asp.net 核心 6 webapp 中抛出异常

[英]HttpContext.Response.WriteAsync("<h1>hello world</h1>") onGet handler method throw Exception in asp.net core 6 webapp

HttpContext.Response.WriteAsync(<h1>hello world</h1>") onGet handler method throws Exception in asp.net core 6 webapp
  1. Create asp.net core web app in asp.net 6在asp.net 6中创建asp.net核心web app
  2. Update the "OnGet" handler method of Index.cshtml.cs file with below code使用以下代码更新 Index.cshtml.cs 文件的“OnGet”处理程序方法
     HttpContext.Response.WriteAsync("<h1>hello world</h1>")
  1. Run the application and observe the console window for error.运行应用程序并观察控制台 window 是否有错误。
Code Details
-------------
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace ResponserWrite.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {
            HttpContext.Response.WriteAsync("<h1>hello world</h1>");
        }
    }
}

Error Details:错误详情:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request.失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常。 System.InvalidOperationException: Headers are read-only, response has already started. System.InvalidOperationException:标头是只读的,响应已经开始。

It seems that you have a misconception how Razor pages app works.您似乎误解了Razor 页应用程序的工作原理。 The actual response is generated by the framework using your model ( Something.cshtml.cs ) and the "view"/"page" ( Something.cshtml ).实际响应是由框架使用您的 model ( Something.cshtml.cs ) 和“视图”/“页面”( Something.cshtml ) 生成的。 In the model in corresponding methods you need to load/manage the required data and in the page file you change it's representation via modifying output html.在 model 的相应方法中,您需要加载/管理所需的数据,并在页面文件中通过修改 output html 来更改其表示。

In case if you try to write the response manually it will cause the aforementioned exception when the framework will try to start the response too.如果您尝试手动编写响应,当框架也尝试启动响应时,会导致上述异常。

If you want to add some html to index page - just open Index.cshtml and add it somewhere.如果您想将一些 html 添加到索引页面 - 只需打开Index.cshtml并将其添加到某处。

Read more:阅读更多:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用`HttpContext.Response.WriteAsync()`后,ASP.NET Core Web API 无法返回 JSON 格式的对象? - ASP.NET Core Web API cannot return JSON formatted object after a call to `HttpContext.Response.WriteAsync()`? 按钮单击无法使H1可见(asp.net) - Button Click doesn't work to make a H1 visible (asp.net) 你是如何创造一个 <h1> 在ASP.NET中以编程方式标记? - how do you create an <h1> tag programmatically in ASP.NET? 使 <h1></h1> 通过C# - Make <h1> </h1> by C# 单击锚链接时未调用 ASP.Net Core 6.0 OnGet 处理程序 - ASP.Net Core 6.0 OnGet handler not called when an anchor link is clicked 如何获取 onget 中的 ID 和 asp.net 核心 razor 页面中的使用处理程序? - How do I get the ID in onget and the use handler in asp.net core razor page? ASP.NET Core API Controller:Response.Body.WriteAsync base64字符串无法正常工作 - ASP.NET Core API Controller: Response.Body.WriteAsync base64 string not working ASP.NET 异常时Core HttpContext.Response.StatusCode为200 - ASP.NET Core HttpContext.Response.StatusCode is 200 when exception 获取下拉选择值并发送到asp.net核心web应用程序中同一页面的OnGet方法 - Get the dropdown selected value and send it to OnGet method of the same page in asp.net core web application Asp.Net Core 2 中是否有等同于“HttpContext.Response.Write”的东西? - Is there an equivalent to "HttpContext.Response.Write" in Asp.Net Core 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM