我有一个带有ControlsLangJsFile方法的名为dictionary的ASP.NET MVC控制器。方法返回包含JavaScript变量的用户控件(ASCX)的视图。
当我调用该方法时,它返回包含已解析字符串的变量,但Content Type为html/text。应该是:application/x-javascript
public ActionResult ControlsLangJsFile()
{
return View("~/Views/Dictionary/ControlsLangJsFile.ascx",);
}
我该如何实现这一点?
发布于 2010-12-14 21:26:27
用户控件不接受文本“ContentType=/xml”
解决方案:
public ActionResult ControlsLangJsFile()
{
Response.ContentType = "text/javascript";
return View("~/Views/Dictionary/ControlsLangJsFile.ascx");
}
发布于 2013-04-10 23:40:42
在构建包含JS的剃须刀视图时,我遇到了同样的问题,并尝试使用@jmav的解决方案:
public ActionResult Paths()
{
Response.ContentType = "text/javascript"; //this has no effect
return View();
}
当您返回一个View()时,这不起作用。似乎视图呈现本身设置了内容类型,而不管在控制器方法中分配了什么。
相反,应该在视图代码本身中进行赋值:
// this lives in viewname.cshtml/vbhtml
@{
this.Response.ContentType = "text/javascript";
}
// script stuff...
发布于 2010-12-14 20:56:11
如下所示,只需相应地更改内容类型:
https://stackoverflow.com/questions/4439355
复制相似问题