Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 400
kidoman Avatar asked Dec 10 '10 17:12

kidoman


People also ask

Is Cshtml the same as Razor?

razor helps you embed serverside code like C# code into web pages. cshtml is just a file extension. razor view engine is used to convert razor pages(. cshtml) to html.

What is _ViewImports Cshtml for?

The purpose of the _ViewImports. cshtml file is to provide a mechanism to centralise directives that apply to Razor pages so that you don't have to add them to pages individually. The default Razor Pages template includes a _ViewImports. cshtml file in the Pages folder - the root folder for Razor pages.

What can the @page directive do in a Razor page?

The Razor content page requires the @page directive at the top of the file. The HasFormContentType property is used to determine whether a form has been posted and the Request. Form collection is referenced within a Razor code block with the relevant value within it assigned to the name variable.


2 Answers

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

public ActionResult MyAction() {
    Response.ContentType = "something";
    return View();
}
like image 110
marcind Avatar answered Oct 19 '22 22:10

marcind


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.

like image 43
Luis Perez Avatar answered Oct 19 '22 23:10

Luis Perez