Giter Site home page Giter Site logo

urlcombine's Introduction

UrlCombine Build Status

The UrlCombine is a Nuget Package to conveniently combine your base Url and relative url. This library treats the slashes between the base and relative paths to ensure a valid url.

  using UrlCombineLib;

There are 3 ways to use the library:

Static Method

The static method is used with UrlCombine helper class. It doesn't matter if the baseUrl and relativePath end/start with a slash or not, The Combine() method takes care of that:

  var fullUrl = UrlCombine.Combine("www.foo.com.br/", "/bar/zeta");
  // fullUrl = "www.foo.com.br/bar/zeta"

String Extension

The string extesion is used with the CombineUrl method:

  var fullUrl = "www.foo.com.br/".CombineUrl("/bar/zeta");
  // fullUrl = "www.foo.com.br/bar/zeta"

Uri Extension

The Uri extension is used with the Combine method:

  var fullUrl = new Uri("www.foo.com.br/").Combine("/bar/zeta");
  // fullUrl = new Uri("www.foo.com.br/bar/zeta")

Multiple relative paths in runtime

There's also a UrlCombine.Combine overload (alongside Uri and String extension methods) that takes a param string[] as input:

  var fullUrl = new Uri("www.foo.com.br/").Combine("bar", "zeta");
  // fullUrl = new Uri("www.foo.com.br/bar/zeta")

Why not Uri(Uri base, string relative)?

Well, It does more than just validate the slashes. It strips the relative path of the base Uri if it doesn't end with a slash and if the relative path doesn't start with one:

  var uriBase = new Uri("http://www.foo.com/relative");
  var relative = "/other/url";
  
  var uri = new Uri(uriBase, relative);
  // uri.ToString() = "http://www.foo.com/other/url"

urlcombine's People

Contributors

jean-lourenco avatar

Watchers

 avatar

Forkers

pataphysicien

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.