-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom ActionFilterAttribute on Razor PageModel Get method #2981
Comments
@fmaeseele page filters can only be applied to a page model, and not individual handler methods. Additionally, you need to use |
Razor Page FiltersAnalogous to action filters, public class SamplePageFilter : IPageFilter
{
public void OnPageHandlerSelected(PageHandlerSelectedContext context)
{
// do something during page handler selection.
}
public void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
// do something after handler selection, but before a handler method executes.
}
public void OnPageHandlerExecuted(PageHandlerExecutedContext context)
{
// do something after a handler method executes.
}
} The public class Index : PageModel
{
public string Title { get; private set; }
public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
Title = "Title set in handler executing";
}
} Page filters can be applied globally by adding to public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Filters.Add(new CustomPageFilter());
});
} They may also be applied to a subset of pages by using public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddFolderApplicationModelConvention("/MyDir", model => model.Filters.Add(new CustomPageFilter()));
});
} |
Hi @pranavkm , Thank you very much for your quick and clear answer. That said, this is unfortunate that we cannot filter on a specific handler method by putting the attribute directly on it. Thanks to RazorPages, we may not need controller in most of the case. So it could be useful to be able to use a kind of action attribute on a specific method handler. Thanks again. Best Regards, |
Razor pages is a recommended way of development... You just have to rewrite all your staff - filters, attributes and so on... :( |
How do I suppress exception?
produces empty page. |
Hi,
I'm trying to get a custom ActionFilterAttribute to get working on the Get method of my Razor Pages.
Starting with a simple .NetCore Web project, I created a custom ActionFilterAttribute as :
`
public class MyCustomActionFilterAttribute : ActionFilterAttribute
{
///
/// Title of the current item
///
public string Title { get; set; }
`
Then I use it in my Index.cshtml.cs :
public class IndexModel : PageModel { [MyCustomActionFilter(Title ="My Title", Order =1)] public void OnGet() { } }
But the method OnActionExecuting is never called.
What am I missing ?
Regards.
François
TestCustomActionFilterAttribute.zip
The text was updated successfully, but these errors were encountered: