Is there an Razor method that will allow to put "object to string" inside html like:
Anchor
Or do I have to use something like that and change my model?
c# object to string to display it in text format
Thanks
if the link you are passing from your model is a string the correct syntax would be
Anchor
or
<a href=#Html.Raw("http://www." + #Model.x.y))>Anchor</a>
This way you are correctly parsing any special characters which might occur in the link like & or =
Related
Problem - The query string parameter value is getting truncated if I have # in the parameter string value.
I have a table where I am binding the anchor tag <a/> prop href value like below;
<a class="btn btn-primary" href="/FileDetails/DownloadBlob?dirName=#data.FolderName&blobName=#data.FileName">
after binding on browser:
<a class="btn btn-primary" href="/FileDetails/DownloadBlob?dirName=RSHSYNCADE&blobName=J231132 anyfoostringnjectable barfoorBstringch #212422627, string Batch #145876V.pdf">
In the above anchor tag for href prop value I am setting a Controller/Action along with the query string parameters dirName and blobName.
But when the control comes to the specified action method of the controller it truncates the second param value from # means I can only see a value upto J231132 anyfoostringnjectable barfoorBstringch.
While trying to find a fix on internet, I am unable to find a proper solution which fit to my scenario till now.
Can anyone please help me to understand what causing this issue and what would be the fix for these kind of issues?
You need to encode the url that you put in the anchor href. If that values comes from the server, use HttpServerUtility.UrlEncode when you set the href tag. Otherwise you can use encodeURIComponent function in javscript or replace the "#" with "%23"
Query string URL must be encoded, there is some special characters for instance if you put plus(+) in query string. It will be handle as space because handler
will detect it as an encoded string then will replace it with space.
Encoded form of plus(+) is %2B
Encoded form of sharp(#) is %23
if you send the sharp value as %23 will handle as a sharp(#) and you won't have problem.
As helper you may use URi builders. There is many NuGet package exist.
in asp.net core view,you could try with asp-route-yourkeyor asp-all-route-data
I tried as below :
<a asp-action="AnotherAction"asp-route-atm="999#999">somelink</a>
Result:
In my controller action, I am sending a message to a view -
model.Result = ex.Message;
model.Result = model.Result + #" If you would like to create one, please click here ";
So, I want the message to show up like so -
The member doesn't exist. If you would like to create one, please click here.
But what actually shows up is
The member doesn't exist. If you would like to create one, please <a href> click here </a>.
So, I have 2 questions -
1) Why is the verbatim string literal not evaluating the html.
2) Is there a better way of doing this? I don't like having html in my controllers, but I also don't want to over architect things for a line of html.
Razor automatically HTML-encodes strings for security (to prevent script injection). You need to use Html.Raw if you want the actual HTML.
#Html.Raw(Model.ResultMessage)
How can I validate Razor template, which is used to render mail template.
string template = #"
My template
#if (Model.Condition { <span>is true</spaM> }"
I made two mistakes: missing closing bracket and wrong closing tag.
I need to validate it and known what should I fix (the best is to known in which line). I would like to use native Razor methods.
If I understand correctly, you want to be notified that the code you've written in the Template is invalid HTML.
If so, I'm afraid there is no easy way. The Template is purely producing text that you specify to go out to the response.
It may not even be HTML - could be JavaScript or a number of other outputs - it's just a string of text.
You may have a Template that produces the start of a table, another that produces the body, and another that produces the footer and end table tags. Each of these would produce invalid HTML on their own, but output one after the other would produce a valid HTML table. (That's a lot of produces there - sorry).
What would make it invalid is the parser of the HTML - i.e. the browser. You would only be able to validate your Template output when it is in a complete document that can then be parsed.
You mean ?
#{
string template = Model.Condition ? "My template <span>is true</span>" : "";
}
string MyString = string.empty;
#if(Model.Condition)
{
MyString ="<span>"+ "is true"+"</span>";
}
I have a text field on my web app. In this field is a url address that user can specify.
If a user sets an incorrect url address that is prefixed with a space, " www.blablabla.xyz" for example, I want to ignore this and correct url will be set ("www.blablabla.xyz"). The * value is presenting some method.
I use <%: Html.* %> conditions and I hold this set URL there.
Imagine this is something like that: <%: Html.*(doom =>doom.MyURL)%>
MyURL is a string value. So is there any method to ignore whitespaces in this case? If yes, how can i do it?
I tried add .Trim() after doom.MyURL (doom.MyURL.Trim()) but this give me an error..
ADD:
I think maybe I can edit the declaration of MyURL in .cs file?
Now MyURL is defined as:
public string MyURL { get; set;}
Is there any easy option to specify this "ignore" here in .cs ?
Try the following, which will help you to find white space in your string and replace it with non white space.
yourString.Replace(" ","");
Try this one,
yourString.TrimStart(string.WhiteSpace);
I am trying to get a page title from page source of different pages. But lets say some pages have title like this:
"This is an example," ABC.
It has some html in it like """. If i use string in c# to get this title i get the whole thing and while displaying it displays it like above which is wrong. Is there any way to ignore or to take into account html values in c#?
I am also using htmlagilitypack so anything in that will do too.
You can use WebUtility.HtmlDecode to decode html, link on MSDN:
WebUtility.HtmlDecode(""This is an example," ABC.");
just use:
using System.Net;
The result will be: "\"This is an example,\" ABC."
You also can use HtmlEntity.DeEntitize in HTML Agility Pack:
HtmlEntity.DeEntitize(string text)
You don't know what you can find in the page title. Sometimes is a whole mess there. My suggestion is to get the string as it is and process it before to show/save it.
In this case, the solution is simple: replace the
"
with corresponding char.
Each time you read a HTML document to extract some tags, take care to tags never closed. If the user forget to close the title tag... you'll get in that line the whole page!