0

I'm looking to write some C# that will detect a piece of a URL and change some text based on the URL found on Page Load.

Essentially, I want this to happen:

  1. URL is detected as ending with ".us || .com" OR ".ca"
  2. Change the text of an <h1> to say "United States" or "Canada" based on the URL found.

My C# experience with this sort of code is almost 0, so I don't have any code to show, because I don't know where to start. Can anyone help me get in the right direction?

Damon Bauer
  • 2,718
  • 1
  • 22
  • 35

5 Answers5

3

You can use the Uri class and view the Host property to see if it contains .us, .com, or .ca with a simple string EndsWith operation.

Please keep in mind that when constructing the Uri class, you must pass in a valid URI.

Matthew
  • 24,703
  • 9
  • 76
  • 110
2

This question might help you in the right direction: Top level domain from URL in C#

Then all you have to do is find the extension, wrap it up in a switch statement or something and wo what you want!

Community
  • 1
  • 1
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
1

If you are inpecting strings that match certain conditions then I would take a look at Regular Expressions.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

Dr. Paul Jarvis
  • 256
  • 3
  • 15
0

you can achieve this by using JQuery too http://jsfiddle.net/tEqwb/11/

$(document).ready(function() {
    var pathname = window.location.pathname;
    if($(pathname +"*.us") || $(pathname +"*.com") || $(pathname +"*.ca"))
    {
      $("#theH1").text("United States");
    }
});​
HatSoft
  • 11,077
  • 3
  • 28
  • 43
0

Try this, it should work

if (ss.Contains(".us"))
{
//code for changing name to US
}
else if (ss.Contains(".ca"))
{
//code for changing name to canada
}

NG.
  • 5,695
  • 2
  • 19
  • 30