Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 966 karma points c-trib
    Oct 06, 2014 @ 07:59
    Murray Roke
    0

    Validating for URL path segment length

    Hi
    A user received this error when trying to view a page he just created:

    PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
    

    The node name was very long.

    I suggested they set a shorter urlName, but is there some way to validate that either the node name is less than 260 OR the UrlName is less than 260 characters?

    I guess I can set up a publish event handler to deal with it and cancel publish.

    Should this be something that umbraco does out of the box?

  • Murray Roke 503 posts 966 karma points c-trib
    Oct 06, 2014 @ 08:47
    Murray Roke
    0

    ok, here's what I did Document.BeforePublish += ValidateUrlSegmentLength;

            private void ValidateUrlSegmentLength(Document sender, PublishEventArgs e)
            {
                var urlName = DocumentExtensions.GetPropertyAsString(sender, "umbracoUrlName");
                if (urlName.HasValue() == false && sender.Text.Length > 200)
                {
                    e.Cancel = true;
                    var page = HttpContext.Current.Handler as Page;
                    if (page != null)
                    {
                        page.PreRenderComplete += (s, a) => umbraco.BasePages.BasePage.Current.ClientTools.ShowSpeechBubble(
                                                                umbraco.BasePages.BasePage.speechBubbleIcon.error,
                                                                "Not Published", "Please add a shorter UrlName"
                                                            ); 
                    }
                }
            }
    

    I also added a regex validation to umbracoUrlName .{0,200}

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Oct 06, 2014 @ 08:47
    Dave Woestenborghs
    1

    I think this should be validated out of the box. There is already a issue for this in the issue tracker : http://issues.umbraco.org/issue/U4-1955

    You can vote this up.

  • Murray Roke 503 posts 966 karma points c-trib
    Oct 06, 2014 @ 08:51
    Murray Roke
    0

    Voted.

  • Moran 285 posts 934 karma points
    Dec 29, 2014 @ 07:25
    Moran
    0

    Hi Can you explain how to implement this solution? do I need to inherit from "ApplicationEventHandler"? Thanks I am using umbraco version 7.1.4

  • Murray Roke 503 posts 966 karma points c-trib
    Dec 29, 2014 @ 10:22
  • Moran 285 posts 934 karma points
    Dec 29, 2014 @ 17:53
    Moran
    0

    Thanks I am using ver 7.1.8 and I think the problem is solved in this version. I tried really long urls an didn't get any YSOD :)

  • Moran 285 posts 934 karma points
    Feb 17, 2015 @ 08:40
    Moran
    0

    Hi I am using umbraco 7.2.1 and I got the issue again, can anyone help me with the implementation of the method above? I keep getting messages for using deprecated methods and classes this is the code:

     public class UrlValidation : ApplicationEventHandler
        {
            public void UrlValidationEvent()
            {
                Document.BeforePublish += ValidateUrlSegmentLength;
            }
    
            private void ValidateUrlSegmentLength(Document sender, PublishEventArgs e)
            {
                var urlName = DocumentExtensions.GetPropertyAsString(sender, "umbracoUrlName");
                if (urlName.HasValue() == false && sender.Text.Length > 200)
                {
                    e.Cancel = true;
                    var page = HttpContext.Current.Handler as Page;
                    if (page != null)
                    {
                        page.PreRenderComplete += (s, a) => umbraco.BasePages.BasePage.Current.ClientTools.ShowSpeechBubble(
                                                                umbraco.BasePages.BasePage.speechBubbleIcon.error,
                                                                "Not Published", "Please add a shorter UrlName"
                                                            );
                    }
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft