asp.net-core - 我可以为 Blazor 服务器页面设置 mime 类型吗?
问题描述
在传统的 ASP.NET 中,我可以这样做:
Response.Clear();
Response.ContentType = "text/plain";
Response.Write("{'some': 'json'}");
Response.End();
我可以在 Blazor 服务器页面中执行类似的操作吗?我只想去某个路线并返回一个字符串而不是html。
还是我最好研究一下在 ASP.NET Core 3 中创建 Web API 的选项?
解决方案
啊哈!我找到了 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
...
推荐阅读
- postgresql - 无论如何在 GORM 中预览将在迁移期间运行的模式 DDL,然后再运行它?
- go - 为什么导入“k8s.io/client-go/kubernetes/fake”时编译失败
- python - Pandas 数据帧初始化性能
- ruby-on-rails - 如何从 Rails 应用程序执行 curl
- javascript - Javascript 模拟时钟时针不工作,而其他指针工作
- git - 我们可以在没有使用 Git 的 pull/checkout/fetch 的情况下跟踪远程引用吗?
- node.js - 如果我有前端和后端的单独文件夹,如何部署 MEAN 应用程序?
- mysql - mysql错误用户'root'@'localhost'的访问被拒绝
- np - 需要帮助来理解通过从哈密顿路径减少来证明最长路径问题是 NP 完全的
- java - Java JScrollpane - JTextPane - Log4J AppenderSkeleton 水平滚动问题