C# razor replace string

Hi guys,

I'm updating an old site into C# Razor, and the links have a URL rewrite function in place using global.asax.

On the old site we have the following code rewriting the URL using the title in the database:

<a href="newsitem/<%= replace(replace(rsNews("title")," ", "_"), "/", "(slash)") %>.html" title="<%=rsNews("Title")%>"><%=rsNews("Title")%></a>

Open in new window


Is there an equivalent to this using C# Razor? Any help/pointers would be much appreciated!
RossLiversidgeAsked:
Who is Participating?
 
unknown_routineCommented:
replace(rsNews("title")," ", "_")

becomes:


rsNews("title").Replace(" ","_");


and then

replace(replace(rsNews("title")," ", "_"), "/", "(slash)")

becomes:

rsNews("title").Replace(" ","_").Replace("/", "(slash"));


Explanation:

rsNews("title") is an instance of string class. string has Replace method. And since rsNews("title") is also a string we can use it's replace method.
0
 
RossLiversidgeAuthor Commented:
Absolutely spot on.

For reference, in Razor I set the code as follows:

<a href="@row.Title.Replace(" ","_").Replace("/", "(slash")).html">
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.