Did you ever need to compare two web address and extract the relative part of them?
For example if we have address "http://foo.com" and "http://foo.com/car/color/power" we would like to get "car/color/power". But if we would have "http://foo.com/car" and "http://foo.com/car/color/power" we would like to get "color/power".
For this cases we should use "MakeRelativeUri". This method is part of Uri class and determines the delta between two URL address. This simple method extract the difference from two URL.
Code examples:
This method can be very useful in certain situations, when you know about it. In one project I saw an extension implemented by developers that was doing the same thing :-).
For example if we have address "http://foo.com" and "http://foo.com/car/color/power" we would like to get "car/color/power". But if we would have "http://foo.com/car" and "http://foo.com/car/color/power" we would like to get "color/power".
For this cases we should use "MakeRelativeUri". This method is part of Uri class and determines the delta between two URL address. This simple method extract the difference from two URL.
Code examples:
Uri uri = new Uri("http://foo.com");
Uri result = Uri.MakeRelativeUri("http://foo.com/car/color/power");
// result: "car/color/power"
Uri uri = new Uri("http://foo.com/car");
Uri result = Uri.MakeRelativeUri("http://foo.com/car/color/power");
// result: "color/power"
Uri uri = new Uri("http://foo.com");
Uri result = Uri.MakeRelativeUri("http://foo.com/car/color/power/index.html");
// result: "car/color/power/index.html"
Uri uri = new Uri("http://foo.com/car/color/power/");
Uri result = Uri.MakeRelativeUri("http://foo.com/");
// result: "../../../"
Uri uri = new Uri("http://foo.com");
Uri result = Uri.MakeRelativeUri("http://secondFoo.com/car/color/power");
// result: "http://secondFoo.com/car/color/power"
When the base address of the second URL is not the same with the first one (base one), the call of this method will return the second URL that was send as parameter.This method can be very useful in certain situations, when you know about it. In one project I saw an extension implemented by developers that was doing the same thing :-).
Comments
Post a Comment