Reputation: 4611
I have a few hundred static HTML files that need to be processed.
They contain links like this
<a href="http://www.mysite.com/">Link</a>
I need to add ?ref=self to any url that begins with http://www.mysite.com
and becomes
<a href="http://www.mysite.com/?ref=self">Link</a>
however, I do not know whether it's going to be http://www.mysite.com
or http://www.mysite.com/
also it could be linked to a sub directory.
What's the most efficient way to do this? in C#
Upvotes: 2
Views: 852
Reputation: 410
You could use Page.Request.UrlReferrer to detect where the request came from.
Upvotes: 0
Reputation: 90012
What's the most efficient way to do this? in C#
http://www.mysite.com
."
.?ref=self
before the "
.This can be accomplished with the following regular expression substitution:
s#http://www.mysite.com[^"]*#&?ref=self#g
A nicer (more expressive) way would be to use an HTML parser and XPath.
Upvotes: 1
Reputation: 5899
Parsing HTML can be tricky as HTML often contains poorly formed tags and attributes. I suggest looking into an existing HTML parsing library to do your heavy lifting, or, using XSLT to transform valid (x)HTML to your desired output.
This question What is the best way to parse html in C#? has some good links to HTML parsing libraries for C#.
Upvotes: 1