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);
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:
I m trying to click on value (link format) on a webpage and i read it from a separate file. the value is read correctly from the external file but I cant get the script to click on it. the first line of code reads the value correctly from the external file but the second is supposed to click on the rendered value. So for instance, the text file has one value of X1 and the webpage has a value in link format called X1. So the idea is to click on the X1 link using the variable valueID rather than just reading the text link from the page. any idea how to implement it or get it to work with the code below please?
<pre>
string ValueID = System.IO.File.ReadAllText(#"ValueID.txt");
await _page.ClickAsync("Value=ValueID");
</pre>
The following code should work:
string ValueID = System.IO.File.ReadAllText(#"ValueID.txt");
await _page.ClickAsync($"text={ValueID}");
Notice that you are including the string interpolation operator inside your string, whereas it should be in front of it, e.g. $"text={ValueID}" instead of "text=${ValueID}", but if the above doesn't work, for some reason, also try the following:
when you're reading the value from the text file, make sure there are no leading or trailing whitespace characters
make sure the actual text value of the element does not contain any leading or trailing whitespace characters
is the entire text value of the element exactly equal to the value of your ValueID string? if not, maybe consider using the :has-text() Playwright pseudo-selector, e.g. await _page.ClickAsync($":has-text('{ValueID}')"); which not only is case-insensitive, but also matches on substrings
maybe try using the href attribute paired with a CSS contains instead, e.g. await _page.ClickAsync($"[attribute*={ValueID}]");
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 =
Please help me to get query string value from the URL.
http://test.com/test.aspx#id=test
I tried to access with
Request.QueryString["id"]
Its getting null value.Please suggest how to access id from the url.
Thanks
I agree with everyone that the # should be a ?, but just FYI:
Note it isn't actually possible get the anchor off the URL, for example:
http://test.com/test.aspx#id=test
The problem is that # specified an anchor in the page, so the browser sees:
http://test.com/test.aspx
And then looks in the page for
<a id="test">Your anchor</a>
As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off.
If you want the part after the # you have to copy it using Javascript before the request is sent to the server, and put the value in the querystring.
More info here c# get complete URL with "#"
A query string starts with a question mark ? not a hash #.
Try:
http://test.com/test.aspx?id=test
Using a hash, you're asking to jump to a named anchor within the document, not providing a query string
Your URL is not valid.
http://test.com/test.aspx#id=test
refers to a bookmark named id=test.
You should use
http://test.com/test.aspx?id=test
And then Request.QueryString["id"] will work.
If you would like to use it as hash tag you can use:
string value = Request.Url.ToString().Split('#')[1];
with this code, you will have your hash tag value.
Isnt it supposed to be?
http://test.com/test.aspx?id=test
I have a function that builds an anchor tag. The function recieves the URL, Title as parameters. The problem is that sometime the text includes quotation marks and this results in a anchor tag generated with syntax errors.
Which is the best way to solve this problems? Is there any function that parses the text into a safe string, in this case, for the title attribute.
Otherwise I can check the string and strip all quotation marks, but I would like know if there is a better way to do this, e.g there might be some other characters that can crash my function as well.
Actually you want to use HttpUtility.HtmlAttributeEncode to encode your title attribute. The other encoders will do more work (and have different uses) whereas this one only escapes ", &, and < to generate a valid text for an attribute.
Example:
This is a <"test"> & something else. becomes This is a <"Test"> & something else.