title: Open an SPWeb from a single Url
body: <p>When I want to get to a web, I usually have to do code like the following which is fairly self explanatory.</p>
  
  <pre><code>using (SPSite site = new SPSite(siteUrl))
              {
                  SPWeb web = null;
  
                  if (string.IsNullOrEmpty(webName))
                      web = site.RootWeb;
                  else
                      web = site.AllWebs[webName];
                  ...
                  web.Close();
              }
  </code></pre>
  
  <p>Given a url that points directly to a web:</p>
  
  <p>So in this case siteUrl would be: <a href="http://localhost/sites/testsite" rel="noreferrer">http://localhost/sites/testsite</a></p>
  
  <p>and webName would be: testWeb</p>
  
  <p>I would like to simply get a single bit of information from the user opposed to these two seperate bits, ie the url directly to the web: <a href="http://localhost/sites/testsite/testweb/" rel="noreferrer">http://localhost/sites/testsite/testweb/</a></p>
  
  <p>I would like to use this url to open the web and not have to specify the web name manually. I've played with site.OpenWeb and tried passing the url to this too but it doesn't like that. It only wants a server relative url. 
  Is there a way to just be able to get a single url from the user in order to open the web, short of pulling the url apart and making assumptions that the last bit may or may not be the name of the web depending on whether we're going to the root web or not but then that makes the code even worse.</p>