A useful UrlBuilder class
翻译:小新0574
原文地址: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 many 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 various 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.
作为一个ASP.NET开发者,构建URLs是一件总是要做的事情。.NET Framework已经宽泛地提供了许多类来帮助我们简化问题,但是在我全面的查询以后,我发现.NET Framework里漏了一种可以轻松地处理QueryString参数的方法。我到处看了看,发现了由不同人写的对于这个问题的解决方案,但是却没有一个建立于已存在在framework里的东西。Framework提供了十分有用的System.UriBuilder
类,但是除能恢复包含 URI 的查询部份的字符串之外,却没有内建的方式处理QueryString参数。虽然对于它本身来说并不是那么有用,但它却能帮助我们创建一个继承类来扩展UriBuilder
的功能。输入UrlBuilder
。其实UriBuilder
是一个由现代命名惯例给出的更加精确的名字,UrlBuilder
在语义上相等,更加有地位,没有任何一个双重的名字在目前的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 should be immediately obvious how it all hangs together. All I shall attempt to do here is give a brief explanation of how to use the UrlBuilder
class 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.
如果读者想更深入到代码里面以了解它内部的工作情况,那么它已经提供了下载以供您精读(译注:请到原文页面下载),代码的组织方式是显而易见的。我在这试着想要做的是通过一些有希望给出轮廓和重点的代码对怎么使用UrlBuilder
类给出一个简明的解释。那么,就别再拖了,让我们开始吧。
The UrlBuilder
class can be instantiated in several ways, but for simplicity, let's assume the user wishes to use a string as the initialiser of this UrlBuilder
instance.
UrlBuilder
类能以几种方式实例化,为了简化一些,让我们假设用户希望使用一个字符串作为这个UrlBuilder
实例的初始化器。
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 base UriBuilder
class with the exception of one additional which takes a System.Web.UI.Page
object. This is useful when one wishes to merely redirect to the same page with different QueryString parameters. The object can also be initialised using a System.Uri
object. Once instantiated, we can manipulate any part of the URI (see the System.Uri
documentation).
在类里提供的其余的构造器重载,除了有一个接受System.Web.UI.Page
之外,跟基类UriBuilder
是一模一样的。当有人仅仅只是想使用QueryString参数重定向到相同的页面,这就很有用。这个对象也可以使用一个System.Uri
对象初始化。一旦实例化,我们就能操作URI的任何一部分了(参见System.Uri
文档)
builder.Path = “archive/2005”;
It is interesting to note that a forward slash “/” is optional at the start of the Path string. Either way, the UriBuilder
class will return a correctly formed URI. The UriBuilder
class provides no way to set only the page name portion of the URI, so I added a new property called PageName
which allows you to set only the name of the required page.
有意思的的是注意在路径开始的前一个斜杠“/”是可选的。或者说,UriBuilder
类将会返回一个正确形式的URI。 UriBuilder
类没有提供方法仅仅设置URI的页面名字部分,所以我增加了一个属性叫做PageName
,他允许你仅仅设置所需页面的名字。
So far, apart from the PageName
property, this is nothing new; all this can already be done with the UriBuilder
class. The real usefulness of the UrlBuilder
class becomes apparent when we try to manipulate the QueryString parameters, as follows:
到目前为止,除了PageName
属性,没什么新东西;所有这些都可以由UriBuilder
类完成。UrlBuilder
类真正的有用之处会在我们试着操作QueryString参数的时候变得明显起来,就像这样:
builder.QueryString[“a”] = “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 internal StringDictionary
object used to store the QueryString pairs are made available to the user. You could, for example, remove one of the parameters:
如果QueryString已经包含包含在传递给构造器的URI里的参数(在这里是“cat”),参数的值会被新值覆盖。如果参数并不存在,就追加到产生的QueryString。另外,用来储存QueryString对(pairs)的内部StringDictionary
对象的所有属性和方法对用户是可用的。比如说,你能删除某一个参数:
Or check if the collection contains a given key or value:
或者检查集合是否包含一个给定的键或值:
builder.QueryString.ContainsValue(“12345”);
Lastly, there are two ways in which the URI may be consumed. Firstly, by calling:
最后,有两种方式,URI可能被用到。第一,通过调用:
Or simply by calling:
或者简单地调用:
which will perform a redirect to the URI currently contained in the object.
这将执行重定向到包含在当前对象中的URI的动作。
Happy coding.
开心编程!