actionfilterattribute

Redirect after Session Timeout in MVC Action Filter

馋奶兔 提交于 2021-01-29 08:02:18
问题 I have to redirect to home page after session timeout in mvc 5 application. I am using following code : public class SessionExpireAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var currentEnvironment = EnvironmentUtility.GetSession().Get<Environment>(); if (currentEnvironment == null) { filterContext.Result = new RedirectResult("~/Home/Index"); } base.OnActionExecuting(filterContext); } } In the controller, I have below code

How to unit test ActionFilterAttribute

半世苍凉 提交于 2020-04-05 17:48:32
问题 I'm looking to test an ActionFilterAttribute in a .NET Core 2.0 API project and wondering the best way to go about it. Note, I'm not trying to test this through a controller action, merely test the ActionFilterAttribute itself. How might I go about testing this: public class ValidateModelAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { context.Result = new BadRequestObjectResult(context.ModelState);

If I wanted to use attributes to muck about with an MVC return contentType, would it be better to do it in OnResultExecuting or OnResultExecuted?

Deadly 提交于 2020-01-15 10:09:57
问题 In the pursuit of solving an absurd problem, I need to append a value to my querystring (did that in javascript) and test if it exists on the server (because this may come from ajax or an iframe, so there's no header potential, sadly, and I'm trying to avoid adding a value to my <form> element). To that end, I've devised this little snippet, and I'm not sure where to put the "setter" block: using System.Web.Mvc; namespace Company.Client.Framework.Mvc { class CreateFormDialogResponseAttribute

Async action filter in MVC 4

倾然丶 夕夏残阳落幕 提交于 2020-01-08 21:52:48
问题 I have an action filter that when used in certain specific conditions has to perform a web service call to ensure that the current state is valid. This initially seemed like an ideal candidate for async/await, but I have encountered a snag: Assume a request to: /Test/FilteredAction MyCustomActionFilter begins executing The first "await" statement is reached TestController.FilteredAction starts executing MyCustomActionFilter resumes executing Traditionally I would expect the action filter to

Async action filter in MVC 4

こ雲淡風輕ζ 提交于 2020-01-08 21:51:52
问题 I have an action filter that when used in certain specific conditions has to perform a web service call to ensure that the current state is valid. This initially seemed like an ideal candidate for async/await, but I have encountered a snag: Assume a request to: /Test/FilteredAction MyCustomActionFilter begins executing The first "await" statement is reached TestController.FilteredAction starts executing MyCustomActionFilter resumes executing Traditionally I would expect the action filter to

Async action filter in MVC 4

你离开我真会死。 提交于 2020-01-08 21:51:35
问题 I have an action filter that when used in certain specific conditions has to perform a web service call to ensure that the current state is valid. This initially seemed like an ideal candidate for async/await, but I have encountered a snag: Assume a request to: /Test/FilteredAction MyCustomActionFilter begins executing The first "await" statement is reached TestController.FilteredAction starts executing MyCustomActionFilter resumes executing Traditionally I would expect the action filter to

Async action filter in MVC 4

本秂侑毒 提交于 2020-01-08 21:51:07
问题 I have an action filter that when used in certain specific conditions has to perform a web service call to ensure that the current state is valid. This initially seemed like an ideal candidate for async/await, but I have encountered a snag: Assume a request to: /Test/FilteredAction MyCustomActionFilter begins executing The first "await" statement is reached TestController.FilteredAction starts executing MyCustomActionFilter resumes executing Traditionally I would expect the action filter to

ASP MVC ActionFilterAttribute OnActionExecuting not fired

对着背影说爱祢 提交于 2020-01-02 17:12:38
问题 I have 2 controllers Home with public class HomeController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { // do some irrelevant stuff base.OnActionExecuting(filterContext); } public ActionResult Index() { return View(); } } and Service with public ActionResult Confirm() { return RedirectToAction("Index", "Home");} And one ActionFilterAttribute with OnActionExecuting method public class InvitationModeAttribute : ActionFilterAttribute { public

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

How do I add a parameter to an action filter in asp.net?

折月煮酒 提交于 2019-12-29 05:16:05
问题 I have the following filter attribute, and i can pass an array of strings to the attribute like this [MyAttribute("string1", "string2")] . public class MyAttribute : TypeFilterAttribute { private readonly string[] _ids; public MyAttribute(params string[] ids) : base(typeof(MyAttributeImpl)) { _ids = ids; } private class MyAttributeImpl : IActionFilter { private readonly ILogger _logger; public MyAttributeImpl(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<MyAttribute>();