如何更改 ASP.NET MVC 控制器中返回的 ContentType (ActionResult)
我有名为字典的 ASP.NET MVC 控制器,其方法为 ControlsLangJsFile。 方法返回包含 JavaScript 变量的用户控件 (ASCX) 视图。
当我调用该方法时,它返回带有解析字符串的变量,但内容类型是 html/text。它应该是: application/x-javascript
public ActionResult ControlsLangJsFile()
{
return View("~/Views/Dictionary/ControlsLangJsFile.ascx",);
}
我该如何实现这一目标?
I have ASP.NET MVC controller named dictionary with method ControlsLangJsFile.
Method returns view of users control (ASCX) which contain JavaScript variables.
When i call the method it returns variables with parsed strings, but Content Type is html/text. It should be: application/x-javascript
public ActionResult ControlsLangJsFile()
{
return View("~/Views/Dictionary/ControlsLangJsFile.ascx",);
}
How do i achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
用户控件不接受 ContentType="text/xml"
解决方案:
Users control doesn't accept ContentType="text/xml"
Solution:
我在使用 JS 构建剃刀视图时遇到了同样的问题,并尝试使用 @jmav 的解决方案:
当您返回 View() 时,这不起作用。看起来视图渲染设置了内容类型本身,而不管控制器方法中分配了什么。
相反,在视图代码本身中进行分配:
I had this same question while building a razor view with JS in it and attempted to use @jmav's solution:
That doesn't work when you are returning a View(). It seems that the view rendering sets the content type itself despite what is assigned in the controller method.
Instead, make the assignment in the view code itself:
像这样,只需相应地更改内容类型:
ASP.NET MVC 和文本/xml 内容类型
Like this, just change the content type accordingly:
ASP.NET MVC and text/xml content type
尝试:
Try: