0

I have strings like:

http://127.0.0.1:22/Test

Is there some single function maybe using regular expressions that I can use to make remove the colon and port number.

It was previously suggested that I use the following but this is giving me problems if the CurrentUrl is null!

var ip = new Uri((string)Session["CurrentUrl"]);
var ipNoPort = string.Format("{0}://{1}/{2}", ip.Scheme, ip.Host, ip.PathAndQuery);
return Session["CurrentUrl"] == null ? Home() : Redirect((string)ipNoPort);

What I really need is some method that combines the three lines and checks for null also.

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 4
    Why do you need them to be on one line? What exactly will that solve? Chances are that you will end up with a long and complicated line of code that will be very difficult to debug. – Oded Nov 07 '11 at 11:53
  • Indeed, just surround the code with a check for a null CurrentUrl session variable. – Rickjaah Nov 07 '11 at 11:55
  • 1
    possible duplicate of [How can I remove some characters from a C# string?](http://stackoverflow.com/questions/7990920/how-can-i-remove-some-characters-from-a-c-sharp-string) – V4Vendetta Nov 07 '11 at 11:56
  • It is a duplicate of the above but after implementing the above I realize it doesn't work if the string is a null. Looking for some more advice thanks. – Samantha J T Star Nov 07 '11 at 11:57

4 Answers4

1

Just test for nullity before using the session variable:

if(Session["CurrentUrl"] != null)
{
  var ip = new Uri(Session["CurrentUrl"]);
  var ipNoPort = string.Format("{0}://{1}/{2}", ip.Scheme, ip.Host, ip.PathAndQuery);
  return Redirect(ipNoPort);
}

return Home();
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0
url = Regex.Replace(url, @"^(http://([0-9]+\.?)+)(:[0-9]+)", "$1");
Don Tomato
  • 3,311
  • 3
  • 30
  • 48
  • This will break if there's some ":123" that's NOT describing the port - e.g. something like `http://sample.com/post.asp?text=My+telephone:555-123`! – Mario Nov 07 '11 at 12:03
0

You can't do it all in one line (you could, but would still make no difference code wise). So you won't get around checking for null first.

To remove the port (not sure if that's wise to do, depends on your layout):

string url = (string) Session["CurrentUrl"];
string clean_url = url != null ? Regex.Replace(url, "://([^/:]+:\\d+)", "://$1") : Home();
Mario
  • 35,726
  • 5
  • 62
  • 78
0

Just to add a little to Oded's answer.

This following line

var ip = new Uri((string)Session["CurrentUrl"]);

will break if Session["CurrentUrl"] is

  1. Null (System.ArgumentNullException)
  2. Not a string (System.InvalidCastException)
  3. Not a URL including an empty string. (System.UriFormatException)

Oded's answer takes care of the first and possibly the only possible cause of an exception in your code, however unless you can be absolutely sure that its always either null or a URL then you might be better off with the following

var currentUrl = Session["CurrentUrl"] as string;

if (!string.IsNullOrEmpty(currentUrl))
{
    try
    {
        var ip = new Uri(currentUrl);
        var ipNoPort = string.Format("{0}://{1}/{2}", ip.Scheme, ip.Host, ip.PathAndQuery);
        return Redirect(ipNoPort);
    }
    catch (System.UriFormatException)
    {
        return Home()
    }
}
else
{
        return Home();
}

another option is to just catch the three exceptions at once but its more expensive if it happens a lot.

If you use this a lot it might be worth while to create a UrlTryParse method.

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155