如何设置WebMatrix/Razor响应的内容类型?

Joh*_*han 18 content-type webmatrix razor

我想在我的WebMatrix cshtml文件中返回一些XML而不是HTML?如何更改内容类型标题?

Joh*_*han 24

使用.cshtml文件顶部的Response.ContentType属性,然后在视图内容中包含XML:

@{ 
   Response.ContentType = "application/xml";
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial>415-123-4567</Dial>
</Response>
Run Code Online (Sandbox Code Playgroud)

  • 哇!!你编辑了你的答案以匹配我的而不是选择我的答案?那很冷,男人......冰冷.(检查每个人的修订) (3认同)

Mik*_*ike 19

在Razor文件的顶部,设置Response对象的ContentType:

@{
  Response.ContentType = "application/xml";
}
... xml here ...
Run Code Online (Sandbox Code Playgroud)

  • @John如果以上工作对你有用,你会考虑改变接受的答案吗?上述情况可能更正确,但两者都达到了预期的效果. (4认同)