一些非常基本的问题。.Net MVC何时使用OnActionExecuting?
何时初始化或分配OnActionExecuting?
很简单的场景:
public partial class OrderController : DefaultController
{
private int customerId = 0;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
customerId = WebSecurity.CurrentUserId;
base.OnActionExecuting(filterContext);
}
}
我应该分配中的customerId OnActionExecuting,所以我可以在每个动作重复使用它的代码?
- 可读性
- 消除重复
或者我应该只是每次给它分配在不同的行动?
- 我们不需要这个赋值或者在每个动作中初始化某个对象的情况下)?
最好的方式看起来像CustomAuthorizatoinAttribute和重写授权() –