Damon Bauer
Damon Bauer

Reputation: 2726

Change text based on URL

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?

Upvotes: 0

Views: 410

Answers (5)

NG.
NG.

Reputation: 6053

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
}

Upvotes: 0

HatSoft
HatSoft

Reputation: 11201

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");
    }
});​

Upvotes: 0

Matthew
Matthew

Reputation: 25773

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.

Upvotes: 3

Dr. Paul Jarvis
Dr. Paul Jarvis

Reputation: 256

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

Upvotes: 1

Gerald Versluis
Gerald Versluis

Reputation: 34013

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!

Upvotes: 2

Related Questions