ChildActionOnly + ActionName的用法(转)

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

<a href="javascript:;" onclick="javascript:document.getElementById('show').style.display=''">
    调用子操作</a>
<div id="show" style="display: none">
<% 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"); %>

 

出处:http://www.cnblogs.com/gengaixue/archive/2012/05/28/2522823.html

posted @ 2014-05-08 14:26  邹邹  Views(197)  Comments(0)    收藏  举报