Codeproject-a useful urlbuilder class

Source: Internet
Author: User
A useful urlbuilder class
By
Lotuspro

A simple way to generate and manipulate URLs.

Translation: xiaoxin 0574
Address: http://www.codeproject.com/aspnet/UrlBuilder.asp

Introduction

Building URLs is something one does all the time as an ASP. net developer. the. net Framework has generously provided into classes to help us simplify the problem, but after all my searching, the one thing I found missing from the Framework was a way to easily work with querystring parameters. I did a little looking around and found a few solutions to the problem written by varous people but none of them built on anything already in the framework. the framework provides the very useful System. uribuilder Class but it has no in-built way to deal with querystring parameters apart from being able to get back a string containing the query portion of the URI. while this is not all that useful in itself, it does help us create a derived class which extends the functionality in Uribuilder . Enter Urlbuilder . While Uribuilder Is a more accurate name given modern naming conventions, Urlbuilder Is semantically equivalent and more importantly, not a duplicate name of anything already in the framework.

As ASP. NET Developer, build URLs It is always something to do. . NET Framework Many classes have been provided to help us simplify the problem, but after my comprehensive query, I found that . NET Framework Which can be easily processed Querystring Parameter Method. I looked around and found a solution for this problem written by different people, but none of them were created in Framework . Framework Provides very useful System. uribuilder Class, but can restore the include Uri In addition to the strings in the query part, there is no built-in processing method. Querystring Parameters. Although it is not so useful for itself, it can help us create an inherited class to expand Uribuilder . Input Urlbuilder . Actually Uribuilder Is a more accurate name given by modern naming conventions, Urlbuilder In terms of semantics, it is more important to have a status. There is no double name in the current Framework .

If the reader wishes to get elbow deep in the code to discover its inner workings, it is provided in the download for your perusal and shoshould be immediately obvious how it all hangs together. all I shall attempt to do here is give a brief explanation of how to useUrlbuilderClass by means of some sample code which will hopefully outline and highlight the salient features of this class. So then, without further delay, let's delve in.

If you want to go deeperCodeTo understand the internal work of the code, you can download the code on the original page. What I'm trying to do here is how to use it through some code that hopefully gives outlines and highlightsUrlbuilderClass provides a concise explanation. Let's get started.

TheUrlbuilderClass can be instantiated in several ways, but for simplicity, let's assume the user wishes to use a string as the initialiser of thisUrlbuilderInstance.

UrlbuilderThe class can be instantiated in several ways. to simplify it, let's assume that the user wants to use a string asUrlbuilder The initialization tool of the instance.

Urlbuilder =   New  
Urlbuilder ("http: // Www.codeproject.com/index.asp? Cat = 4 ");

The rest of the constructor overloads provided in the class are exactly the same as those of the baseUribuilderClass with the exception of one additional which takesSystem. Web. UI. PageObject. This is useful when one wishes to merely redirect to the same page with different querystring parameters. The object can also be initialised usingSystem. UriObject. Once instantiated, We can manipulate any part of the URI (seeSystem. UriDocumentation ).

The remaining constructors provided in the class are overloaded, except for one that accepts System. Web. UI. Page And the base class Uribuilder Is exactly the same. When someone just wants to use Querystring It is useful to redirect parameters to the same page. This object can also use System. Uri Object initialization. Once instantiated, we can operate Uri (See System. Uri Document)

Builder. Host = "Www. Gibbons. co. za ";
Builder. Path = "Archive / 2005 ";

It is interesting to note that a forward slash "/" is optional at the start of the path string. Either way,UribuilderClass will return a correctly formed URI.UribuilderClass provides no way to set only the Page name portion of the URI, so I added a new property calledPagenameWhich allows you to set only the name of the required page.

It is interesting to note that the previous slash" / "Is optional. Or, Uribuilder Class will return a correct form Uri . Uribuilder Class does not provide a method to set only Uri Page name, so I added an attribute called Pagename He allows you to set only the name of the required page.

Builder. pagename = " 06 . Aspx ";

So far, apart fromPagenameProperty, this is nothing new; all this can already be done withUribuilderClass. The real usefulness ofUrlbuilderClass becomes apparent when we try to manipulate the querystring parameters, as follows:

So far,PagenameAttribute, there is nothing new; all of these can beUribuilder Class.Urlbuilder The real usefulness of the class will be implemented when we tryQuerystringThe parameters become obvious, as shown in the following code:

Builder. querystring ["cat"] =   12345 ;
Builder. querystring ["A"] = "";
Builder. querystring ["B"] = "B ";

If the querystring already contains a parameter contained in the URI passed to the constructor (in this case "CAT"), the value of the parameter will be overwritten with the new value. if the parameter doesn' t already exist, it is appended to the querystring generated. in addition, all the properties and methods of the internalStringdictionaryObject used to store the querystring pairs are made available to the user. You cocould, for example, remove one of the parameters:

If Querystring Included in Uri Parameters in (here is" Cat ), The parameter value will be overwritten by the new value. If the parameter does not exist, append it to the generated Querystring . In addition, it is used to store Querystring Pair ( Pairs ). Stringdictionary All attributes and methods of the object are available to users. For example, you can delete a parameter:

Builder. querystring. Remove ("cat ");

Or check if the collection contains a given key or value:

Or check whether the set contains a given key or value.:

Builder. querystring. containskey ("cat ");
Builder. querystring. containsvalue (" 12345 ");

Lastly, there are two ways in which the URI may be consumed. Firstly, by calling:

Finally, there are two methods,UriIt may be used. First, call:

String Uri = Builder. tostring ();

Or simply by calling:

Or simply call:

Builder. navigate ();

Which will perform a redirect to the URI currently contained in the object.

This redirects the execution toUri.

Happy coding.

Happy programming!

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.