【问题标题】:Send Html in .Net Core 2.2在 .Net Core 2.2 中发送 Html
【发布时间】:2019-02-26 08:51:02
【问题描述】:

我试图在“启动”类中的这一行获取 html(在浏览器中)。但不将其仅显示为纯文本。 ON 本地主机 https://localhost:44378/(应用程序默认)

它在 .net core 2.2 中

public void Configure(IApplicationBuilder app)
{
    app.Run(async (context) =>
    {
        //send html here  but shows simple text
        await context.Response.WriteAsync("htmls here <br>");
    });
}

【问题讨论】:

  • “但不工作” 不是对问题的技术描述。事实上,它不是对任何事物的描述。请尽最大努力描述正在发生的事情、您的期望以及您期望的原因
  • 为什么要在 Startup 类中发送 HTML?更重要的是,您希望将 HTML 发送给谁?
  • @Michael Randall 它按原样显示简单的文本
  • @MindSwipe:代码不是在 Startup 中发送 html,.Run 配置了一个响应(在特定情况下)每个请求的中间件。 kunal:您需要设置正确的内容类型标题。
  • @kunalverma 你需要做context.Response.Headers.Add("Content-Type", "text/html"); 你不能设置标题集合

标签: c# asp.net .net asp.net-core .net-core


【解决方案1】:

有一个简单的解决方法,很简单@Tseng 在 cmets 中指出:您需要将 Response 的 Content-Type 标头设置为 text/html,您可以这样做:

app.Run(async (context) =>
{
    // Or you could use indexer like so: context.Response.Headers["Content-Type"] = "text/html"
    context.Response.Headers.Add("Content-Type", "text/html");
    await context.Response.WriteAsync("htmls here <br>");
});

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2020-08-25
    • 2019-09-29
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    相关资源
    最近更新 更多