呼吁JsonResult @ Html.Action改变父模板我响应类型

编程入门 行业动态 更新时间:2024-10-16 20:32:03
本文介绍了呼吁JsonResult @ Html.Action改变父模板我响应类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下控制器:

公共类的HelloController{    公众的ActionResult指数()    {        返回查看()    }    公众的ActionResult你好()    {        返回JSON(新{=问候你好,世界!},JsonRequestBehavior.AllowGet);    }}

然后,在 Index.cshtml :

... HTML东西<脚本类型=文/ JavaScript的>    警报(@ Html.Action(你好));< / SCRIPT>

什么我发现是,在我的浏览器去这个网址时,响应内容类型为应用程序/ JSON的;字符集= UTF-8 这会导致浏览器呈现HTML作为字符串而不是作为...网页。

什么是解决这个问题的最好方法?

解决方案

之所以这是所有 Html.Action 调用是直接执行。是这样的:

  • 索引被称为
  • 执行中查看结果
  • 您好被执行的动作,设定的ContextType
  • 索引视图结果返回
  • 浏览器显示的页面
  • 您有两个选择:

  • 打破了产生逻辑世界,你好!成常规的C#类,并在索引控制器动作直接调用它
  • 加载通过AJAX的Hello操作,然后显示警报。
  • 选项1

    公共类的HelloController{    YourBusiness _yb;    公共HelloController中(YourBusiness YB)    {        _yb = YB;    }    公众的ActionResult指数()    {        返回查看(yb.GenerateHello())    }    //用于一切,但指数    公众的ActionResult你好()    {        返回JSON(新{问候= yb.GenerateHello()},JsonRequestBehavior.AllowGet);    }}公共类YourBusiness{    公共字符串GenerateHello()    {        返回你好世界国际!;    }}

    选项2

    <脚本类型=文/ JavaScript的>    $获得(@ Url.Action(你好)',函数(响应){        警报(response.greeting);    }< / SCRIPT>

    边注

    Internet Explorer中相当积极,当涉及到缓存。该JSON响应将被改变。因此,我建议你也指定没有缓存为JSON动作:

    [的OutputCache(持续时间= 0,NoStore = TRUE)]公众的ActionResult你好(){    返回JSON(新{=问候你好,世界!},JsonRequestBehavior.AllowGet);}

    I've got the following controller:

    public class HelloController { public ActionResult Index() { return View() } public ActionResult Hello() { return Json(new{ greeting = "hello, world!" }, JsonRequestBehavior.AllowGet); } }

    Then, inside Index.cshtml:

    ...html stuffs <script type="text/javascript"> alert("@Html.Action("Hello")"); </script>

    What I'm finding is that, when going to this url in my browser, the response content type is application/json; charset=utf-8 which causes the browser to render the html as a string instead of as... a web page.

    What's the best way to get around this?

    解决方案

    The reason to this is that all Html.Action invocations are executed directly. Something like:

  • Index is called
  • View result is executed
  • Hello action is executed, set's ContextType
  • Index view result is returned
  • Browser displays the page
  • You got two options:

  • Break out the logic which generates "Hello world!" into a regular C# class and invoke it directly in the Index controller action
  • Load the Hello action through ajax and then display the alert.
  • Option 1

    public class HelloController { YourBusiness _yb; public HelloController(YourBusiness yb) { _yb = yb; } public ActionResult Index() { return View(yb.GenerateHello()) } // used for everything but Index public ActionResult Hello() { return Json(new{ greeting = yb.GenerateHello() }, JsonRequestBehavior.AllowGet); } } public class YourBusiness { public string GenerateHello() { return "Hello wolrd!"; } }

    Option 2

    <script type="text/javascript"> $.get('@Url.Action("Hello")', function(response) { alert(response.greeting); } </script>

    Side note

    Internet Explorer is quite aggressive when it comes to caching. The JSON responses will be changed. I therefore recommend that you also specify no cache for the JSON action:

    [OutputCache(Duration = 0, NoStore = true)] public ActionResult Hello() { return Json(new{ greeting = "hello, world!" }, JsonRequestBehavior.AllowGet); }

    更多推荐

    呼吁JsonResult @ Html.Action改变父模板我响应类型

    本文发布于:2023-11-25 23:04:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1631704.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:模板   类型   JsonResult   Html   Action

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!