开发者

asp.net logic for fixing relative urls to full urls

i have a function that pulls URLs from various web resources. needless to say some are full valid URLS and some are relative as per the HTML of the page. below is my asp.net/ c# logic i derived for examining the URL and then generate a full usable URL from whats pulled from the site...

I have have not looked at this code in some time but i remember it was working well some months ago and now it needed many tweaks to get running - especially with relative paths and the regeneration of a FULL url from various relative variations.

is there a simpler way or method to accomplish this seemingly routing boilerplate task than what i have here?

NOTE: origianlurl is the full url of the first searched page, and relativeUrl is a url found within the searched page (it can be a full www.site.com or a /contactus.html)

    private string ResolveRelativePaths(string relativeUrl, string originatingUrl)
    {
        if (relativeUrl.StartsWith("http") || relativeUrl.StartsWith("www"))
            return relativeUrl;

        if (relativeUrl.StartsWith("/"))
        {
            //get main url something.com
            Uri myURI = new Uri(originatingUrl);

            //add the relative page to the end
            return myURI.Host + relativeUrl;
        }

        string resolvedUrl = String.Empty;

        string[] relativeUrlArray = relativeUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        string[] originatingUrlElements = originatingUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        int indexOfFirstNonRelativePathElement = 0;
        for (int i = 0; i <= relativeUrlArray.Length - 1; i++)
        {
            if (relativeUrlArray[i] != "..")
            {
                indexOfFirstNonRelativePathElement = i;
                break;
            }
        }

        int countOfOriginatingUrlElementsToUse = originatingUrlElements.Length - indexOfFirstNonRelativePathElement - 1;
        //for (int i = 0; i <= countOfOriginatingUrlElementsToUse - 1; i++)
        for (int i = 0; i <= countOfOriginatingUrlElementsToUse ; i++)
        {
            if (originatingUrlElements[i] == "http:" || originatingUrlElements[i] == "https:")
                resolvedUrl += originatingUrlElements[i] + "//";
            else
                resolvedUrl += originatingUrlElements[i] + "/";
        }

        for (int i = 0; i <= relativeUrlArray.Length - 1; i++)
        {
            if (i >= indexOfFirstNonRelativePathElement)
            {
                resolvedUrl += relativeUrlArray[i];

                if (i < relativeUrlArray.Length - 1)
                    resolvedUrl += "/";
            }
        }

        return resolvedUrl;
    } 
开发者_如何学Go


The Uri class has a constructor that you can use for that exactly. Given a base uri, which is your originatingUrl and a string (the relative part) it generates the full url. As far as I can see there is not a single thing in your method that cannot be done using the Uri class (maybe a few instances). My guess is that you could rewrite it to 5-10 LOC.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜