1、 普通方式
public class HomeController : Controller
{
public void Index()
{
}
}
对应的是:http://***/home/index.rails, vm文件对应views/home/index.vm{
public void Index()
{
}
}
2、 ControllerDetails方式
[ControllerDetails("Extended2")]
public class ExtendedController : Controller
{
public void Save()
{
throw new Exception();
}
}
对应的是:http://***/Extended2/Save.rails, vm文件对应views/Extended2/Save.vmpublic class ExtendedController : Controller
{
public void Save()
{
throw new Exception();
}
}
3、 Area方式
[ControllerDetails(Area="subarea")]
public class HomeController : Controller
{
public void Index()
{
}
}
对应的是:http://****/subarea/home/Index.rails, vm文件对应views/subarea/home/Index.vmpublic class HomeController : Controller
{
public void Index()
{
}
}
4、 Default动作
可以设置:
[DefaultAction("Index")]
public class ServletController : Controller
{
public void Index(int id, string name, int age, DateTime birthday)
{
}
}
那么当所有访问http://***/servlet/aa.rails时,当ServletController中没有对应方法时,都会自动执行Index方法 (vm文件对应views/servlet/aa.vm)public class ServletController : Controller
{
public void Index(int id, string name, int age, DateTime birthday)
{
}
}
5、 动态动作
[DynamicActionProvider( typeof(DynamicActionProvider1) )]
public class DynController : Controller
{
}
public class DynamicActionProvider1 : IDynamicActionProvider
{
public void IncludeActions(Controller controller)
{
controller.DynamicActions["index"] = new IndexDynamicAction();
}
}
public class IndexDynamicAction : IDynamicAction
{
public void Execute(Controller controller)
{
controller.PropertyBag.Add("message", "hello!");
}
}
对应的是:http://****/dyn/Index.rails, vm文件对应views/dyn/Index.vmpublic class DynController : Controller
{
}
public class DynamicActionProvider1 : IDynamicActionProvider
{
public void IncludeActions(Controller controller)
{
controller.DynamicActions["index"] = new IndexDynamicAction();
}
}
public class IndexDynamicAction : IDynamicAction
{
public void Execute(Controller controller)
{
controller.PropertyBag.Add("message", "hello!");
}
}
那么对于某一个action在很多Controller中都会用到的情况,就可以把这个action定义在DynamicAction中
本文转自永春博客园博客,原文链接:http://www.cnblogs.com/firstyi/archive/2007/10/26/939057.html,如需转载请自行联系原作者