代码之家  ›  专栏  ›  技术社区  ›  John Sheehan

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

  •  18
  • John Sheehan  · 技术社区  · 15 年前

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

    3 回复  |  直到 13 年前
        1
  •  24
  •   John Sheehan    13 年前

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

    @{ 
       Response.ContentType = "application/xml";
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Dial>415-123-4567</Dial>
    </Response>
    
        2
  •  19
  •   Mike    14 年前

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

    @{
      Response.ContentType = "application/xml";
    }
    ... xml here ...
    
        3
  •  0
  •   Luis Perez    13 年前

    如果您使用ASP.NET MVC,您可以选择在控制器中更改操作方法,如下所示:

    public ActionResult MyAction() {
        Response.ContentType = "text/xml";
        return View();
    }
    
    推荐文章