How to change returned ContentType in ASP.NET MVC controller (ActionResult)

jmav picture jmav · Dec 14, 2010 · Viewed 35.2k times · Source

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?

Answer

jmav picture jmav · Dec 14, 2010

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

Solution:

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