首页 > 解决方案 > 我可以为 Blazor 服务器页面设置 mime 类型吗?

问题描述

在传统的 ASP.NET 中,我可以这样做:

Response.Clear();
Response.ContentType = "text/plain";
Response.Write("{'some': 'json'}");
Response.End();

我可以在 Blazor 服务器页面中执行类似的操作吗?我只想去某个路线并返回一个字符串而不是html。

还是我最好研究一下在 ASP.NET Core 3 中创建 Web API 的选项?

标签: asp.net-coreblazor-server-side

解决方案


啊哈!我找到了 Scott Hanselman 的一个例子。它向您展示了如何生成robots.txt带有.cshtml页面的动态文件。

以下是将上述代码重写为.cshtml文件:

@page "/file.txt"

@{
    Layout = null;
    this.Response.ContentType = "text/plain";
    var bytes = System.Text.Encoding.ASCII.GetBytes( "{'some': 'json'}" );
    await this.Response.Body.WriteAsync( bytes );
}

我假设您不能对.razor页面执行此操作-因为文件将呈现组件_Host.cshtml...


推荐阅读