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

前端 未结 4 832
半阙折子戏
半阙折子戏 2021-01-03 21:13

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

When

相关标签:
4条回答
  • 2021-01-03 21:43

    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...
    
    0 讨论(0)
  • 2021-01-03 21:44

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

    Solution:

    public ActionResult ControlsLangJsFile()
        {
            Response.ContentType = "text/javascript";
            return View("~/Views/Dictionary/ControlsLangJsFile.ascx");
        }
    
    0 讨论(0)
  • 2021-01-03 21:52

    Like this, just change the content type accordingly:

    ASP.NET MVC and text/xml content type

    0 讨论(0)
  • 2021-01-03 21:54

    Try:

    return Json(new
    {
          uCode = SysContext.CurrentUserCode,
          uPwd = SysContext.CurrentUserPwd,
          rMe = SysContext.RememberMe
    }, "application/json", JsonRequestBehavior.AllowGet);
    
    0 讨论(0)
提交回复
热议问题