0

I have taken over a project that has a lot of code that is concatenating strings to put together a url. I keep struggling to remember if each of these APIs return the "/" at the end of the base Url (either "http://www.mysite.com" or "http://www.mysite.com/")

For example:

var baseUrl = "http://www.mySite.com/";
var controllerAndAction = "/Mycontroller/MyAction";

var fullUrl = baseUrl + controllerAndAction;

or

var baseUrl = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
var controllerAndAction = "Mycontroller/MyAction";

var fullUrl = baseUrl + controllerAndAction;

or

var baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
var controllerAndAction = "Mycontroller/MyAction";

var fullUrl = baseUrl + controllerAndAction;

or

var baseUrl = "http://www.mySite.com";
var controllerAndAction = "/Mycontroller/MyAction";

var fullUrl = baseUrl + controllerAndAction;

(Notice the "/"s at the end of baseUrl and at the beginning of controllerAndAction)

I am trying to figure out the cleanest way to make sure these create proper Urls that include a single "/" (not 2 or 0)

is there anything i can do to help confirm this (besides manually testing each one . .) ?

NOTE:

i need this code to work in an asp.net-mvc and a winforms project so i am trying to avoid code that is coupled to either one (to avoid referencing unnecessary libraries)

leora
  • 188,729
  • 360
  • 878
  • 1,366

4 Answers4

2

That isn't fun. Check out Path.Combine for Urls. This method should allow you to combine base urls, paths and query strings.

Note: The link suggests using a URI which is a base system class, available in any type of C#/VB project.

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

I use Path.Combine .Am not 100% sure that it will handle the missing '/' case, you will need to try it out once.

santiagoIT
  • 9,411
  • 6
  • 46
  • 57
0

Suppose all inputs are correct (all have only 0 or 1 /). I think you need 2 if/else to make sure that:

  1. The end of baseURL has /
  2. The start of controllerAndAction does not have /
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
0

None of the above. Why concatenating strings when the framework already provides you with a method that does this job? You've even tagged your question with string-concatenation, but that's so wrong for what you are trying to do.

To generate an absolute url in an ASP.NET MVC application that takes into account your routes it's as simple as:

public ActionResult Foo()
{
    var fullUrl = Url.Action("MyAction", "MyController", null, Request.Url.Scheme);
    ...
}

You should never use string concatenations and hardcoding when dealing with urls. Always use url helpers when you work with urls in your ASP.NET MVC application.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This code is not in my MVC project. I need this code to work in another solution that is not a web project so i am trying to stay decoupled from asp.net-mvc – leora Dec 26 '11 at 22:14
  • @leora, then why on earth is your question tagged with `asp.net-mvc`? It has nothing to do with it. It confused me. Please tag your question appropriately. Tag it with `winforms` or whatever you are doing but not `asp.net-mvc`. – Darin Dimitrov Dec 26 '11 at 22:18
  • i updated the question to clarify this point . . apologize as i realize that was not clear and thus a time waster for you – leora Dec 26 '11 at 22:25