如何更改 ASP.NET MVC 控制器中返回的 ContentType (ActionResult)

发布于 2024-10-07 12:15:16 字数 355 浏览 2 评论 0原文

我有名为字典的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦爱 2024-10-14 12:15:16

用户控件不接受 ContentType="text/xml"

解决方案:

public ActionResult ControlsLangJsFile()
    {
        Response.ContentType = "text/javascript";
        return View("~/Views/Dictionary/ControlsLangJsFile.ascx");
    }

Users control doesn't accept ContentType="text/xml"

Solution:

public ActionResult ControlsLangJsFile()
    {
        Response.ContentType = "text/javascript";
        return View("~/Views/Dictionary/ControlsLangJsFile.ascx");
    }
鹿港小镇 2024-10-14 12:15:16

我在使用 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...

I had this same question while building a razor view with JS in it and attempted to use @jmav's solution:

public ActionResult Paths()
{
    Response.ContentType = "text/javascript"; //this has no effect
    return View();
}

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:

// this lives in viewname.cshtml/vbhtml
@{
    this.Response.ContentType = "text/javascript";
}
// script stuff...
娇俏 2024-10-14 12:15:16

像这样,只需相应地更改内容类型:

ASP.NET MVC 和文本/xml 内容类型

Like this, just change the content type accordingly:

ASP.NET MVC and text/xml content type

早乙女 2024-10-14 12:15:16

尝试:

return Json(new
{
      uCode = SysContext.CurrentUserCode,
      uPwd = SysContext.CurrentUserPwd,
      rMe = SysContext.RememberMe
}, "application/json", JsonRequestBehavior.AllowGet);

Try:

return Json(new
{
      uCode = SysContext.CurrentUserCode,
      uPwd = SysContext.CurrentUserPwd,
      rMe = SysContext.RememberMe
}, "application/json", JsonRequestBehavior.AllowGet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文