How to concatenate two strings in image source with razor? - asp.net-mvc

I've defined the following variable in razor:
#{string imageRoot = "/_media/Images/";}
I'd like to use it here:
<img src="#imageRoot App1/MyImage.png"/>
The problem is the space within the string.
This will work but I'd like to keep the trailing slash in the variable instead of in the literal:
#{string imageRoot = "/_media/Images";}
<img src="#imageRoot/App1/MyImage.png"/>

Looks a little ugly, but works:
#{string imageRoot = "/_media/Images/";}
<img src="#Html.Raw(imageRoot)App1/MyImage.png" />

Use String.Concat to merge the two string.
#{imgRoot = string.Concat(imgRoot,"/_media/Images");}
or just normal imgRoot + = imgRoot + "/_media/Images"
then set your variable as <img src='#imageRoot'/>
or <img src="#{imageRoot+#"/App1/MyImage.png"}"/>
Note that the #-symbol is a Verbatim string literal and will prevent the \ from being escaped should you have a character that escapes your string

Related

#HttpContext.Current.User.Identity.Name not showing backslash

Super Simple. Only issues I find are people getting null. Which I obvi fixed. But where is the backslash???!!
params.me = '#HttpContext.Current.User.Identity.Name';
This returns
"domainUserName" <- Browser
"domain\\UserName" <- Debugging
What I expect is
"domain\UserName" <- Browser
Any ideas?
Based on your comments you are using the following code to show the user name:
alert('#HttpContext.Current.User.Identity.Name');
#HttpContext.Current.User.Identity.Nameis a string that can contain "\" backslash character. This character is considered as a escape character in javascript as it is in C# as well.
You need to escape the "\" character in the string before passing it to Javascript like that:
alert('#HttpContext.Current.User.Identity.Name.Replace("\\", "\\\\")')

How to deal with dynamic urls with special characters like single quote?

I am generating dynamic an "a href" html tag on my asp page. Also the url is dynamic. Sometimes there are special characters inside the url and the hyperlink is not working. For example when there is an single quote:
http://myCompany.com/'s-hertog.aspx
How can I fix this that the dynamic url always will work?
I already try this, but is not working:
string hyperLinkHtml = string.Format("<span class=\"bw-NewsQueryWebpart-BodyItemTitle\"><a href='{0}' >{1}</a>", HttpUtility.UrlEncode(newsItem.Url), newsItem.Title);
I found the solution by my self. I changed the single quotes to double quotes in the string.format:
string hyperLinkHtml = string.Format("<span class=\"bw-NewsQueryWebpart-BodyItemTitle\"><a href=\"{0}\" >{1}</a>", HttpUtility.UrlEncode(newsItem.Url), newsItem.Title);

How to replace string after extraction from WebHarvest?

I wanted to insert the records I had extracted from website to DB, but the extraction text contained the symbol apostrophe, and had caused me syntax error during sql insertion. May I know how to replace apostrophe with "’" instead in WebHarvest?
Thanks in advance!
I normally use the script element to work on strings, and then output to a new webharvest variable. For example:
<var-def name="r_output">
A long string with lots of funny characters
new lines and & and ' single and " double quotes
</var-def>
<var-def name="r_output2">
<script return="r_output2">
<![CDATA[
String r_output2 = "\n" + r_output.toString().replaceAll("&", "&").replaceAll("\\t","").replaceAll("\\n","").replaceAll("\\r","");
]]>
</script>
</var-def>
<var name="r_output2"/>
as a side note, instead of quoting your apostrophes in quotes it is much better to quote the whole chunk of data eg: "a string with a ' single quote" instead of a string with a "'" single quote

Generated URL by Html.RouteLink with special characters

I have the following line
#Html.RouteLink(type.Description, "ListingsWithTypeSpecified", new { country = Model.CountryCode, state = Model.State, city = Model.CurrentCity.Name.ToLower(), description = type.Description.ToLower(), id = type.TypeID })
which produces
http://some.com:9609/ca/on/london/physiotherapy%20%20%26%20acupuncture/2
and
http://some.com:9609/ca/on/london/health%20department/19
first one has spaces and a &, second one just has a space
for the first one, I would like to still show
http://localhost:9609/ca/on/london/physiotherapy and acupuncture/2
for this one I understand replacing the & with "and" will work, however I still do not want %20 as spaces instead I would like to have a clean url.
Which method should I be using to properly have friendly url shown by what Html.RouteLink creates?
Replace the space with a -
e.g.
type.Description.ToLower().Replace(" ", "-")

# statement in razor templates shows HTML symbols instead of quotes

I've noted that # statement in Razor templates converts quotes in strings into HTML symbols.
How can I show correct HTML attribute then? Sample code:
<body#(ViewBag.Highlight == true ? " onload=\"prettyPrint()\"" : "")>
result:
<body onload="prettyPrint()">
That's completely incorrect. How can i achieve normal:
<body onload="prettyPrint()">
in my case?
I've tried HtmlString object from this answer. But it's impossible to convert HtmlString to string even with explicit type cast.
You will need to use Html.Raw(). Try it this way:
#Html.Raw(String.Format("<body{0}>", ViewBag.Highlight == true ? " onload=\"prettyPrint()\"" : ""))
As the documentation says:
Returns markup that is not HTML encoded.

Resources