English 中文(简体)
How do you set the content type for a WebMatrix/Razor Response?
原标题:

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

最佳回答

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>
问题回答

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

@{
  Response.ContentType = "application/xml";
}
... xml here ...

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();
}




相关问题
REST question: PUT one representation, GET a different one?

Short version of the question: Does "GET" at a particular URI need to match what was "PUT" to that URI? I think not. Here s why: Given that a resource is an abstract thing that is theoretically ...

Sending the variable s content to my mailbox in Python?

I have asked this question here about a Python command that fetches a URL of a web page and stores it in a variable. The first thing that I wanted to know then was whether or not the variable in this ...

SharePoint Multiple New Item Forms

I ve got a custom list with a custom content type. I m aware that when you create a new item you can see a drop down for the different content types on that list which I assume all have their own ...

Downloading pictures/Word documents using ASP.NET

If I put the following code: Response.ContentType = "image/jpeg" Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg") Response.WriteFile(MapPath("capitol.jpg"))...

Detecting custom folder content types in MOSS2007

Given an SPListItem representing a folder, I need to find out whether it has the builtin folder content type, or a custom folder content type (with additional fields). Here is what I do ...

热门标签