开发者

Is a custom attribute right for applying global application checks?

In my application I have the requirement to check if the user has paid the subscription and if not, redirect him to a renew service page.

I have then created a custom attribute class that does the check and if the user has not paid the subscription change the View. Here is the code

public class CheckForActiveServiceAttribute : ActionFilterAttribute {
    public override void OnActionExecuting( ActionExecutingContext filterContext ) {
        if ( !checkForActiveService ) {
            filterContext.Result = new ViewResul开发者_如何学编程t { ViewName = "Cart" };
        }
        base.OnActionExecuting( filterContext );
    }
}

Is this the right approach to follow? Also, how can I create a new model and bind it to the strong-typed view "Cart"?


1) Yeah, why not?

2) You can set the view model using something like the following:

public class CheckForActiveServiceAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!false)
        {
            filterContext.Result = new ViewResult { ViewName = "Cart" };
            ((ViewResultBase)filterContext.Result).ViewData.Model = new MyModel();
        }
        base.OnActionExecuting(filterContext);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜