How do you set the content type for a WebMatrix/Razor Response?

后端 未结 3 1626
失恋的感觉
失恋的感觉 2021-02-19 00:44

I\'d like to return some XML instead of HTML in my WebMatrix cshtml file? How do you change the content type header?

相关标签:
3条回答
  • 2021-02-19 00:52

    Use the Response.ContentType property at the top of your .cshtml file then include the XML in the content of the view:

    @{ 
       Response.ContentType = "application/xml";
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Dial>415-123-4567</Dial>
    </Response>
    
    0 讨论(0)
  • 2021-02-19 01:09

    At the top of your Razor file, set the ContentType of the Response object:

    @{
      Response.ContentType = "application/xml";
    }
    ... xml here ...
    
    0 讨论(0)
  • 2021-02-19 01:10

    If you are using ASP.NET MVC, you can choose to make the change in your action method in the controller, like so:

    public ActionResult MyAction() {
        Response.ContentType = "text/xml";
        return View();
    }
    
    0 讨论(0)
提交回复
热议问题