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

时间:2021-09-19 21:08:19

I have ASP.NET MVC controller named dictionary with method ControlsLangJsFile. Method returns view of users control (ASCX) which contain JavaScript variables.

我有ASP.NET MVC控制器命名字典与方法ControlsLangJsFile。方法返回包含JavaScript变量的用户控件(ASCX)视图。

When i call the method it returns variables with parsed strings, but Content Type is html/text. It should be: application/x-javascript

当我调用该方法时,它返回带有解析字符串的变量,但内容类型是html / text。它应该是:application / x-javascript

public ActionResult ControlsLangJsFile()
    {
        return View("~/Views/Dictionary/ControlsLangJsFile.ascx",);
    }

How do i achieve this?

我如何实现这一目标?

4 个解决方案

#1


29  

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

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

Solution:

解:

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

#2


15  

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

我在使用JS构建剃刀视图并尝试使用@jmav的解决方案时遇到了同样的问题:

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.

当您返回View()时,这不起作用。看起来,尽管在控制器方法中分配了内容类型,但视图呈现仍设置了内容类型。

Instead, make the assignment in the view code itself:

而是在视图代码中进行赋值:

// this lives in viewname.cshtml/vbhtml
@{
    this.Response.ContentType = "text/javascript";
}
// script stuff...

#3


2  

Like this, just change the content type accordingly:

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

ASP.NET MVC and text/xml content type

ASP.NET MVC和text / xml内容类型

#4


1  

Try:

尝试:

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

#1


29  

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

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

Solution:

解:

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

#2


15  

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

我在使用JS构建剃刀视图并尝试使用@jmav的解决方案时遇到了同样的问题:

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.

当您返回View()时,这不起作用。看起来,尽管在控制器方法中分配了内容类型,但视图呈现仍设置了内容类型。

Instead, make the assignment in the view code itself:

而是在视图代码中进行赋值:

// this lives in viewname.cshtml/vbhtml
@{
    this.Response.ContentType = "text/javascript";
}
// script stuff...

#3


2  

Like this, just change the content type accordingly:

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

ASP.NET MVC and text/xml content type

ASP.NET MVC和text / xml内容类型

#4


1  

Try:

尝试:

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