Asp.net

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

  • December 14, 2010

我有名為字典的 ASP.NET MVC 控制器,方法是 ControlsLangJsFile。方法返回包含 JavaScript 變數的使用者控制項 (ASCX) 視圖。

當我呼叫該方法時,它返回帶有解析字元串的變數,但內容類型是 html/text。它應該是:application/x-javascript

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

我如何實現這一目標?

使用者控制項不接受 ContentType=“text/xml”

解決方案:

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

引用自:https://stackoverflow.com/questions/4439355