How to set contenttype in razor (CSHTML)?

2019-03-15 07:50发布

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?

3条回答
走好不送
2楼-- · 2019-03-15 08:28

Use this:

return Content(json, "application/json");
查看更多
女痞
3楼-- · 2019-03-15 08:30

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-03-15 08:36

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

public ActionResult MyAction() {
    Response.ContentType = "something";
    return View();
}
查看更多
登录 后发表回答