Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alessandro 21 posts 51 karma points
    Dec 16, 2013 @ 16:25
    Alessandro
    0

    MVC Filter inside Umbraco 6/7

    Hi

    I've setup a custom filter inside an umbraco installation in this way:

    1) inside global.asax

    public class Global : UmbracoApplication{
    
        protected override void OnApplicationStarting(object sender, EventArgs e) {
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            base.OnApplicationStarting(sender, e);
        }
    }
    

    2) inside App_start/FilterConfig.cs

    public static void RegisterGlobalFilters(GlobalFilterCollection filters){
        filters.Add(new CheckFilterAttribute());
    }
    

    3) inside Filters/CheckFilterAttribute.cs

    public class CheckFilterAttribute : ActionFilterAttribute {
          public override void OnActionExecuting(ActionExecutingContext filterContext) {
                 var dateTemp= DateTime.Now;
      }
    }
    

    and it's works (i've set a breakpoint on var dateTemp= DateTime.Now; and application stop at every request on that breakpoint correctly.

    My question is: there's a way inside a filter to access to Umbraco data? Something like

    DynamicPublishedContentList list = Umbraco.ContentAtXPath("//name", null);
    

    in the same way like in a view?

    thanks a lot

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Dec 16, 2013 @ 17:09
    Jeroen Breuer
    102

    You can do this in your filter:

    protected static UmbracoHelper Umbraco
    {
        get
        {
            return new UmbracoHelper(UmbracoContext.Current);
        }
    } 

    Than you can get the current node like this:

    var node = Umbraco.AssignedContentItem;
    var url = Umbraco.NiceUrl(node.GetPropertyValue<int>("login")); 

    Jeroen

  • jake williamson 207 posts 873 karma points
    Sep 23, 2017 @ 20:10
    jake williamson
    0

    just happened across this and it's dug me out of a hole!

    appreciate that the post is nearly 4 years old but big thanks jeroen ;)

  • Alessandro 21 posts 51 karma points
    Dec 16, 2013 @ 17:19
    Alessandro
    0

    thanks a lot Jeroen

  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Dec 16, 2013 @ 19:26
    Jeroen Breuer
    0

    Hello,

    Could you please mark a post as the solution if it helped you.

    Jeroen

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies