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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Users control doesn’t accept ContentType=”text/xml”
Solution:
public ActionResult ControlsLangJsFile()
{
Response.ContentType = "text/javascript";
return View("~/Views/Dictionary/ControlsLangJsFile.ascx");
}
Method 2
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...
Method 3
Like this, just change the content type accordingly:
ASP.NET MVC and text/xml content type
Method 4
Try:
return Json(new
{
uCode = SysContext.CurrentUserCode,
uPwd = SysContext.CurrentUserPwd,
rMe = SysContext.RememberMe
}, "application/json", JsonRequestBehavior.AllowGet);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0