.net - How to redirect by throwing an exceptions from an Asp.NET controller? -
my controllers extend basic userawarecontroller class, exposes getcurrentuser() method. call method redirect login page if user not logged in.
can accomplish throwing exception method? how can cause redirect happen when exception thrown?
while think should change approach solution authorization, can stay , use code:
public partial class baseuserawarecontroller : controller { protected override void onauthorization(authorizationcontext filtercontext) { if (getcurrentuser() == null) { filtercontext.result = new httpunauthorizedresult(); } } }
if project not big, think changing use [authorize]
. if used it, only:
[authorize] public partial class userawarecontroller : controller { }
you may think not big difference, [authorize]
handles caching issues (returning cached response when not authorized anymore).
install mvc 2 , create new mvc 2 web application. contains authorization logic can propably use in application.
Comments
Post a Comment