action-filter

ASP.NET MVC ActionFilter

五迷三道 提交于 2020-01-01 19:01:10
问题 Does anybody knows if the OnResultExecuted method from ActionFilterAttribute class is executed even in CATCH block? ie: [CookiesActions] public ActionResult Login(Usuarios usuario) [...] return View(new UsersViewModel(sceUsuarios.Usuario,true)); } catch { return View(new UsersViewModel(new Usuarios(),true));//is OnResultExecuted fired here? } [...] 回答1: In short: YES . You can verify this with a a simple logging action filter. public class LogAttribute : ActionFilterAttribute { public

Enable/Disable mvc server side validation dynamically

て烟熏妆下的殇ゞ 提交于 2020-01-01 06:34:26
问题 I have an mvc form with multiple submit buttons - 'Save Draft' and 'Publish'. The objective is to skip both client side(javascript/unobstructive) validation and server side validation when the 'Save Draft' button is clicked and the form is submitted. But I do need to trigger both validation if the 'Publish' button is clicked. My research has led me to few solutions. Client Side -By writing a jquery plugin (function ($) { $.fn.turnOffValidation = function (form) { var settings = form.validate(

Get ActionName, ControllerName and AreaName and pass it in ActionFilter Attribute

倖福魔咒の 提交于 2019-12-31 08:34:22
问题 I use a custom AuthorizationFilter like the followings: public class ActionAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) { if(!httpContext.User.Identity.IsAuthenticated) return false; if(IsUserExcluded()) return false; else return IsRoleAuthorize(httpContext); } } I use this filter at the top of each action I have, and for check Is Authorized, need Action Name, Controller Name, And Area Name. So is there any way to get

Change culture before ModelBinder is used

拥有回忆 提交于 2019-12-30 03:07:24
问题 I want to create a website in different languages. I already read that I could create an ActionFilter, but I have a litte problem: I had to create a custom ModelBinder in order to work with english and german number formats ( 123,456,789.1 vs. 123.456.789,1 ) public class DecimalModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { string key = bindingContext.ModelName; var v = ((string[])bindingContext

Order property of ActionFilter, from lowest to greatest or vice versa?

限于喜欢 提交于 2019-12-30 01:59:07
问题 I defined two ActionFilters: [DefaultResources(Order = 2)] [RenderTemplate(Order = 1)] And to my surprise DefaultResources is executed BEFORE RenderTemplate. But according to MSDN documentation it should work vice versa: [Filter1(Order = 2)] [Filter2(Order = 3)] [Filter3(Order = 1)] public void Index() { View("Index"); } In this example, action filters would execute in the following order: Filter3, Filter1, and then Filter2. I'm using .NET 4. And comparing by method OnActionExecuted. Am I

How can you unit test an Action Filter in ASP.NET Web Api?

血红的双手。 提交于 2019-12-29 10:13:55
问题 I was looking to add an Action Filter to my service to handle adding link data to the response message. I have found that I need to mock HttpActionExecutedContext but it's a difficult class to mock, how are you dealing with Action Filter testing? 回答1: You can create a fake for HttpActionExecutedContext as below: public static HttpActionContext CreateActionContext(HttpControllerContext controllerContext = null, HttpActionDescriptor actionDescriptor = null) { HttpControllerContext context =

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

浪尽此生 提交于 2019-12-27 17:19:46
问题 For multilingual ASP.NET MVC 3 web application, I am determining the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture on the controller factory as follows: public class MyControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { //Get the {language} parameter in the RouteData string UILanguage; if (requestContext.RouteData.Values["language"] == null)

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

一笑奈何 提交于 2019-12-27 17:19:36
问题 For multilingual ASP.NET MVC 3 web application, I am determining the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture on the controller factory as follows: public class MyControllerFactory : DefaultControllerFactory { protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { //Get the {language} parameter in the RouteData string UILanguage; if (requestContext.RouteData.Values["language"] == null)

ASP.NET MVC: Not executing actionfilters on redirect and throw HttpException

爷,独闯天下 提交于 2019-12-24 19:18:42
问题 I've created an OnActionExecuted filter to populate some viewmodel attributes with data from db (I'm not using ViewData["somekey"], preferring many ViewModels descending from a common ancestor). public class BaseController : Controller { protected DataClassesDataContext context = new DataClassesDataContext(); protected override void OnActionExecuted(ActionExecutedContext filterContext) { ViewModel model = (ViewModel) ViewData.Model; model.IsUserAuthenticated = filterContext.HttpContext.User

Using [Authorize] with OpenIdConnect in MVC 6 results in immediate empty 401 response

久未见 提交于 2019-12-23 17:10:04
问题 I'm trying to add Azure AD authentication to my ASP.NET 5 MVC 6 application and have followed this example on GitHub. Everything works fine if I put the recommended code in an action method: Context.Response.Challenge( new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType); However, if I try using the [Authorize] attribute instead, I get an immediate empty 401 response. How can I make [Authorize] redirect properly to Azure AD? My