Resolveurl perfect solution in Asp.net

Source: Internet
Author: User

Based on my personal experience in using ASP. NET and by searching online. I found that the resolveurl method of page has brought us some serious problems.

The most common problem is that it cannot be used outside of a page or control.

Other problems are bugs. For example, it cannot properly process some URLs you have given. For example, tryPage. resolveurl ("~ /Test. aspx? Param = http://www.test.com"). The result is the same as the string you entered without any changes. Use reflector to view Asp.netCode, I found that the relative URL to absolute URL mechanism is to first search for ": //" in the string, if found, it will be returned directly. Therefore, if you pass a parameter with: //, the query string is OK.We all know that the query string parameter should be urlencode, but if not, it should still be accepted URL. Seriously, check your browser.

On the Internet, we recommend using virtualpathutility. toabsolute. If you are passing a URL as a query string, this is quite good and convenient. Otherwise, an exception will occur. If it is an absolute URL, it will also throw an exception!
So I decided to find a final solution.

First, I want to search for a good variable that can be used in the application.ProgramThe virtual path during running. The page context is not used.

I used httpruntime. appdomainappvirtualpath. It can be used anywhere-even in timer callback! It does not have a trailing slash path. ASP. NET does a special processing to remove the slash. However, we can fix it :-)

Then, I did some tests using the original resolveurl code to find out how to replace it with appvirtualpath:

1. When the URL starts with a slash (/or \), it will not be changed!

2. When the URL starts with ~/, it will be replaced by appvirtualpath.

3. When the URL is an absolute URL, it will not be changed..

4. In any other situation (or even starting with ~, rather than a slash), append the URL to the appvirtualpath.

5. Each time it modifies the URL, it also fixes the slash. Delete the double slash and use/to replace \.

Code:

Code

Public   Static   String Resolveurl ( String Relativeurl)
{
If (Relativeurl =   Null ) Throw   New Argumentnullexception ( " Relativeurl " );

If (Relativeurl. Length =   0   | Relativeurl [ 0 ] =   ' / '   |  
Relativeurl [ 0 ] =   ' \\ ' ) Return Relativeurl;

Int Idxofscheme =  
Relativeurl. indexof ( @" :// " , Stringcomparison. ordinal );
If (Idxofscheme ! =   - 1 )
{
Int Idxofqm = Relativeurl. indexof ( ' ? ' );
If (Idxofqm =   - 1   | Idxofqm > Idxofscheme) Return Relativeurl;
}

Stringbuilder sburl =   New Stringbuilder ();
Sburl. append (httpruntime. appdomainappvirtualpath );
If (Sburl. Length =   0   | Sburl [sburl. Length -   1 ] ! =   ' / ' ) Sburl. append ( ' / ' );

// Found question mark already? Query string, do not touch!
Bool Foundqm =   False ;
Bool Foundslash; // The latest char was a slash?
If (Relativeurl. Length >   1
&& Relativeurl [ 0 ] =   ' ~ '
&& (Relativeurl [ 1 ] =   ' / '   | Relativeurl [ 1 ] =   ' \\ ' ))
{
Relativeurl = Relativeurl. substring ( 2 );
Foundslash =   True ;
}
Else Foundslash =   False ;
Foreach ( Char C In Relativeurl)
{
If ( ! Foundqm)
{
If (C =   ' ? ' ) Foundqm =   True ;
Else
{
If (C =   ' / '   | C =   ' \\ ' )
{
If (Foundslash) Continue ;
Else
{
Sburl. append ( ' / ' );
Foundslash =   True ;
Continue ;
}
}
Else   If (Foundslash) foundslash =   False ;
}
}
Sburl. append (C );
}

ReturnSburl. tostring ();
}

After completing the code and comparing the original resolveurl tests again and again, I started to test performance... in most cases, my code execution speed is 2.7 times faster than the original resolveurl! I also perform tests within the loop and run the code 100000 times with different URLs.

Http://www.codeproject.com/KB/aspnet/resolveurl.aspx ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.