action-filter

Understanding ASP.NET MVC Lifecycle: Why is Model not available in ActionFilter?

人走茶凉 提交于 2021-01-29 09:14:50
问题 I have created the following Custom ActionFilter, when I try to access the Model in the following code, it is null: public class CustomPermissionCheckAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { OrganisationBaseController orgBaseController = context.Controller as Controller; var vm = ((Controller)context.Controller).ViewData.Model as MyViewModel; // null // check if current user has permission to vm.OrganisationId base

How to pass a method or parameter to an action filter in ASP.Net MVC

微笑、不失礼 提交于 2020-06-29 04:07:16
问题 I'm going to handle authentication and authorization in an action filter and create an action filter like below: public class Auth : ActionFilterAttribute { public int Access { get; set; } public string Roles { get; set; } = "Default"; public Func<bool> AuthFunc { get; set; } public override void OnActionExecuting(HttpActionContext actionContext) { string UserId = HttpContext.Current.User.Identity.GetUserId(); //Authentication if (Roles != "Default" && UserManager.IsInRole(UserId, Roles)) { /

How to pass a method or parameter to an action filter in ASP.Net MVC

我怕爱的太早我们不能终老 提交于 2020-06-29 04:06:38
问题 I'm going to handle authentication and authorization in an action filter and create an action filter like below: public class Auth : ActionFilterAttribute { public int Access { get; set; } public string Roles { get; set; } = "Default"; public Func<bool> AuthFunc { get; set; } public override void OnActionExecuting(HttpActionContext actionContext) { string UserId = HttpContext.Current.User.Identity.GetUserId(); //Authentication if (Roles != "Default" && UserManager.IsInRole(UserId, Roles)) { /

ASP.Net MVC Action Filter : What is the difference between OnActionExecuting and OnResultExecuting regarding usage

别来无恙 提交于 2020-02-24 10:51:00
问题 basically i am looking for comparison between OnActionExecuting and OnResultExecuting . when we should work with OnActionExecuting and when should work with OnResultExecuting . if anyone know the situation then share the knowledge. thanks 回答1: From Filtering in ASP.NET MVC: Action filters. These implement IActionFilter and wrap the action method execution. The IActionFilter interface declares two methods: OnActionExecuting and OnActionExecuted. OnActionExecuting runs before the action method.

ASP.Net MVC Action Filter : What is the difference between OnActionExecuting and OnResultExecuting regarding usage

吃可爱长大的小学妹 提交于 2020-02-24 10:50:51
问题 basically i am looking for comparison between OnActionExecuting and OnResultExecuting . when we should work with OnActionExecuting and when should work with OnResultExecuting . if anyone know the situation then share the knowledge. thanks 回答1: From Filtering in ASP.NET MVC: Action filters. These implement IActionFilter and wrap the action method execution. The IActionFilter interface declares two methods: OnActionExecuting and OnActionExecuted. OnActionExecuting runs before the action method.

Can a filter access properties from my BaseController?

こ雲淡風輕ζ 提交于 2020-01-24 10:04:48
问题 I have a basecontroller that has a property like: public class BaseController : Controller { public User CurrentUser {get;set;} protected override void OnActionExecuting(ActionExecutingContext filterContext) { // if session cookie found, set User object here } } Now I want to create an action filer that I could set on controllers or actions that I want to do something like: if (User.IsAdmin) { } else { // redirect to login or some page } So this filter @AdminOnly I could put on a controller

Orchard CMS continues executing request after permanent redirect from IActionFilter

隐身守侯 提交于 2020-01-05 10:26:10
问题 We have a website that uses Orchard CMS and we've written a "RedirectFilter" to do permanent redirects on legacy URLs (aliases that don't match the AutoroutePart pattern for the relevant Content Item). The filter implements IActionFilter and inherits FilterProvider . It works fine and when it does a redirect it calls the following method in OnActionExecuting : filterContext.HttpContext.Response.RedirectPermanent(targetPath, true); According to the documentation the second parameter suggests

How to handle Session timeout in MVC 3

风格不统一 提交于 2020-01-02 08:28:09
问题 I am having issues with frequent Session Time Out. I want to write a common filter that I could use on each controller, filter should redirect the user to login and after log in back to from where user sent the last request. 回答1: You could try something like this: public class SessionExpireAttribute : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); } public override void OnActionExecuting

Response.Redirect not working inside an custom ActionFilter

老子叫甜甜 提交于 2020-01-01 19:24:05
问题 My code is the following public class SessionCheckAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (/*condition*/) { filterContext.HttpContext.Response.Redirect("http://www.someurl.com",true); } base.OnActionExecuting(filterContext); } } Now, the question is WHY does the action that is has [SessionCheck] applied to it STILL executes. Any ideas? Thanks. 回答1: Don't use Response.Redirect, rather replace the Result on the

How to automatically overload DELETE and PUT if they are not available by the client?

对着背影说爱祢 提交于 2020-01-01 19:12:08
问题 How can I detect at the startup of the application that a client doesn't support DELETE and PUT verbs and automatically overload the POST verb? On the server side, how can I redirect those overloaded POST verbs into the right actions? Say I have a DELETE request that is overriden, how do I call the appropriate function in the controller that matches the action? My guess is that I should use some action filter and use reflection to inspect the attributes that matches my function (in this