Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

c# help building a link string

Posted on 2009-04-18
5
Medium Priority
?
253 Views
Last Modified: 2013-12-17
Hi

I have made some changes in my blog and want to change the url link for the blog tags.

I almost got it, but cant get the last part

using the code below my tag outputs as

anchor text = Test
and url link = http://www.example.com/appliance/.aspx?brand=Test

The Category part shows
anchor text = Washing Machines
and url link = http://www.example.com/appliance/washing-machines.aspx

I am trying to get the Tag to be like this

anchor text = Test Washing Machines   (so this will be the tag + the Category)

and then the url link to be

http://www.example.com/appliance/washing-machines.aspx?brand=test

so the category gets inserted into the url,, the spaces get replaced with -  and its all lowercase

can anyone help me correct my code below please





/// Displays the Post's categories seperated by the specified string.
		/// </summary>
		protected virtual string CategoryLinks(string separator)
		{
			string[] keywords = new string[Post.Categories.Count];
			string link = "<a href=\"{0}{1}.aspx\">{2}</a>";
            string path = VirtualPathUtility.ToAbsolute("~/");
			for (int i = 0; i < Post.Categories.Count; i++)
			{
				if (Category.Categories.Contains((Category)Post.Categories[i]))
				{
					string category = Category.GetCategory(Post.Categories[i].Id).Title;
					keywords[i] = string.Format(CultureInfo.InvariantCulture, link, path, Utils.RemoveIllegalCharacters(category), category).ToLower();
				}
			}
 
 
			return string.Join(separator, keywords);
		}
 
		/// <summary>
		/// Displays the Post's tags seperated by the specified string.
		/// </summary>
		protected virtual string TagLinks(string separator)
		{
			if (Post.Tags.Count == 0)
				return null;
            string[] keywords = new string[Post.Categories.Count];
            string[] tags = new string[Post.Categories.Count];
         
            string link = "<a href=\"{0}{1}\">{2}</a>";
            string path = Utils.RelativeWebRoot +".aspx?brand=";
            for (int i = 0; i < Post.Categories.Count; i++)
            {
                if (Category.Categories.Contains((Category)Post.Categories[i]))
                {
                    string category = Category.GetCategory(Post.Categories[i].Id).Title;
                  keywords[i] = string.Format(CultureInfo.InvariantCulture, link, path, Utils.RemoveIllegalCharacters(category), category).ToLower();
 
                }
            }
 
			for (int i = 0; i < Post.Tags.Count; i++)
			{
				string tag = Post.Tags[i];
				tags[i] = string.Format(CultureInfo.InvariantCulture, link, path, HttpUtility.UrlEncode(tag), HttpUtility.HtmlEncode(tag));
 
            }
 
			return string.Join(separator, tags);
		}

Open in new window

0
Comment
Question by:sparky74
  • 3
5 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24175537
are you talking about url rewriting
http://msdn.microsoft.com/en-us/library/ms972974.aspx
0
 

Author Comment

by:sparky74
ID: 24175842
Hi

No i'm just trying to add the category value into the url

0
 

Author Comment

by:sparky74
ID: 24175850
my current link looks like

url link = http://www.example.com/appliance/.aspx?brand=Test

but I want to add the category value, for example Washing Machines

so my url link will look like this

url link = http://www.example.com/appliance/washing-machines.aspx?brand=Test

0
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 2000 total points
ID: 24176290
Try this
protected virtual string TagLinks(string separator)
{
   if (Post.Tags.Count == 0)
       return null;
 
  string[] tags = new string[Post.Categories.Count * Post.Tags.Count];
  string link = "<a href=\"{0}{1}.aspx?brand={2}\">{3}{4}</a>";
  string path = VirtualPathUtility.ToAbsolute("~/");
  int k = 0;
  
  for (int i = 0; i < Post.Categories.Count; i++)
  {
    for (int j = 0; j < Post.Tags.Count; j++)
    {
     if (Category.Categories.Contains((Category)Post.Categories[i]))
     {
      string category = Category.GetCategory(Post.Categories[i].Id).Title;
      string tag = Post.Tags[j];
      tags[k] = string.Format(CultureInfo.InvariantCulture, link, path, Utils.RemoveIllegalCharacters(category), tag, tag, category).ToLower();
      k++;
     }
    }
   }
 
   return string.Join(separator, tags);
}

Open in new window

0
 

Author Closing Comment

by:sparky74
ID: 31571803
This was just the help I needed. Thank you so much your code worked a treat!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question