• asp.net mvc ChildActionOnly 和ActionName的用法


    ChildActionOnly的目的主要就是让这个Action不通过直接在地址栏输入地址来访问,而是需要通过RenderAction来调用它。

    <a href="javascript:;" onclick="javascript:document.getElementById('show').style.display=''">

    调用子操作</a>

    <div id="show" >

    <% Html.RenderAction("Test", "ChildTest"); %></div>

    public ActionResult Index()

    {

    return View();

    }

    [ChildActionOnly]

    public ActionResult Test()

    {

    return Content("Hello");

    }

    http://localhost:666/ChildTest/Test

    如果直接这样访问Action的话就会报如下错误:

    操作“Test”只能由子请求访问。

    说明执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
    异常详细信息System.InvalidOperationException: 操作“Test”只能由子请求访问。

    从某种意义上来说也可以增强一定的安全性。

    ActionName的意思就是为Action定义一个新的名称

    [ActionName("NewTest")]

    public ActionResult Test()

    {

    return Content("Hello");

    }

    如果是这样修改后,那么调用的时候就不是Test了,而是NewTest

    <% Html.RenderAction("NewTest", "ChildTest"); %>

    同名的时候不能重载(可以有一个Get,一个Post),

    如:下面两个是可以允许的

    public ActionResult ReadBook()

    {

    }

       [HttpPost]
            public ActionResult ReadBook(int id,int pageNum)
            {
            }

    如果还要定义一样的话,可以通过自定义Action的名字的时候可以的

     [ActionName("Index2")]

     public ActionResult ReadBook(int n,int a,int b)
            {
       }

    转载:http://www.cnblogs.com/monian/p/4118465.html

  • 相关阅读:
    Codeforces Round #647 (Div. 2) E Johnny and Grandmaster
    Codeforces1363E Tree Shuffling
    洛谷P6583 回首过去
    Codeforces 817D Imbalanced Array
    Codeforces 213E Two Permutations
    hdu6312 Game
    树算法笔记(三):Prim最小生成树
    基础数学(四):充分与必要条件
    树算法笔记(二):Kruskal最小生成树
    树算法笔记(一):并查集
  • 原文地址:https://www.cnblogs.com/louby/p/6552450.html
Copyright © 2020-2023  润新知