c#,asp.net-mvc,asp.net-mvc-4,actionfilterattribute,custom-action-filter
There are two approaches for doing this. 1st Approach: one way is making a base controller and inherit your controllers from base which need authenticated user: public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Session["UserName"] == null) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.HttpContext.Response.StatusCode = 403; filterContext.Result...
asp.net,asp.net-mvc,custom-action-filter
Dear friends i solve my question it was just [SetterProperty] missing from the setter property in my LogAttribute Class public class LogAttribute : ActionFilterAttribute { [SetterProperty] public ApplicationDbContext Context { get; set; } public string Description { get; set; } public LogAttribute(string description) { Description = description; } public override...
asp.net-mvc-4,custom-action-filter
usually you don't use action filter for so-called business logic of your web application - this is what the controllers are for. Action filter are rather for the whole stuff which is external to the actual logic - common case is logging, performance measurement, checking if user is authenticated /...
asp.net-mvc,custom-action-filter
Yes you can do it but first you have convert your currentUser property from protected to public or expose it through read only property or method. Then you can access it using the following var baseController = filterContext.Controller as BaseController; if (baseController != null) { //Access your exposed **public** property...
asp.net-mvc,json,exception-handling,custom-action-filter
I finally found the answer to my solution in another Stack Overflow post (Can I return custom error from JsonResult to jQuery ajax error method?). I should use JsonExceptionFilterAttribute as follows: public class JsonExceptionFilterAttribute : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.HttpContext.Response.StatusCode = 500; filterContext.ExceptionHandled...
asp.net-mvc,http-redirect,action-filter,custom-action-filter
For this scenario I would recommend a Filter that derives from AuthorizeAttribute and then override the AuthorizeCore method. As far as I know these kind of Filters are executed before any other filters (includeding those derived from ActionFilterAttribute, like yours). I had a similar scenario like yours and deriving from...