kidoman
kidoman

Reputation: 2422

How to set contenttype in razor (CSHTML)?

In classic ASP.NET Web Forms view engine, we can set the ContentType in the .aspx page to the desired type.

Is there a direct/recommended equivalent in Razor?

Upvotes: 14

Views: 17261

Answers (3)

Leandro Lanceloti
Leandro Lanceloti

Reputation: 9

Use this:

return Content(json, "application/json");

Upvotes: 0

Luis Perez
Luis Perez

Reputation: 28120

That will work I just tested it, you can also add the following line to your cshtml:

Response.ContentType = "application/javascript";

so that it looks something like this:

@{
    ViewBag.Title = "Home Page";
    Response.ContentType = "application/javascript";
}

It just depends on where you prefer to make the change.

Upvotes: 17

marcind
marcind

Reputation: 53183

You should set a different content type in your action method.

public ActionResult MyAction() {
    Response.ContentType = "something";
    return View();
}

Upvotes: 18

Related Questions