How to set contenttype in razor (CSHTML)?

天大地大妈咪最大 提交于 2019-12-31 13:13:17

问题


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?


回答1:


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

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



回答2:


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.




回答3:


Use this:

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


来源:https://stackoverflow.com/questions/4411841/how-to-set-contenttype-in-razor-cshtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!