Following code fetches image URL (imgURL) from sql database in a View:
<img src=#Server.MapPath(item.imageUrl); />
Following Html that it renders for the above line of code in the browser is wrong:
<img src="D:\Visual" studio="" projects\onlinestore\onlinestore\images\products\dry\baking="" goods\dalda.jpg;="">
Correct Html should be:
<img src="D:\Visual studio projects\onlinestore\onlinestore\images\products\dry\baking goods\dalda.jpg;="">
Can anyone please guide?
You have 2 issues.
First the value of the attribute must be quoted (the html you are seeing is because the value contains spaces - at each space, the attribute ends and a new (invalid) attribute is created)
Second, you do not need #Server.MapPath() (and that should not be used in a view anyway). The values of imageUrl property should be in the format
"/images/products/dry/baking goods/dalda.jpg"
and then in the view its
<img src="#item.imageUrl" />
Try
var pathOnDisk = HttpContext.Current.Server.MapPath(uri.AbsolutePath);
var decoded = HttpContext.Current.Server.UrlDecode(pathOnDisk);
Related
How do I create a hyperlink to an image stored in wwwroot on a Razor page?
Example:
#{
var WFR = "~/images/Certifications/" + (#Model.WFR ?? "../LogoSmall.jpg");
<img src="#WFR" asp-append-version="true" /> // Works great!
<a asp-action="#WFR">see document</a> // Fails miserably
}
The img tag renders the image just fine. But the anchor tag prepends a controller/action which routes me to my error page. This is what the tag helper generates:
https://localhost:44394/Employment/~/images/Certifications/12144abb-6af2-424f-b005-4c2039228934_WFR2016.jpg. Setting asp-controller="" just brings me to the home controller.
What am I missing?
You don't need any tag helpers for this, just set the tag to link to your image -
see document
A user in another forum suggest this: Url.Content()
see document
... and it works!!
I'm using ASP.NET Core 2 with Razor to try and pass an image URL into the View via a ViewModel. Unfortunately, the image URL has spaces in it, which I don't have control over at the moment.
I have a file name like this (note the space):
https://example.com/images/file name.jpg
I'm using Razor to try and show the image like this:
<img src=#item.ImageUrl />
The output HTML looks like this:
<img src="https://example.com/images/file" name.jpg />
Obviously this will not do! I should mention, we know this all works when we use an image that has no spaces in the file name. Here are some ways I've tried to correct the issue:
Using Url.Content helper in Razor (this did not make a difference):
<img src=#Url.Content(item.ImageUrl) />
Using System.Web.HttpUtility.UrlEncode when populating the ViewModel (this results in a "double-encoding" error):
myViewModel.ImageUrl = HttpUtility.UrlEncode(someUrl);
My goal is to get the URL encoded properly (no "double-encoding" and no truncating at the space in the input string). How can I achieve this goal?
The problem is that Razor will attempt to create valid HTML, so when it sees a space it automatically closes the quotation marks, so:
#{string img = "a a.jpg";}
<img src=#img />
Gets rendered as
<img src="a "a.jpg /> // of course invalid
So, you need to be sure you include your quotation marks, then Razor will use them:
<img src="#img" />
Which gets rendered as
<img src="a a.jpg" />
You can try
<img src="#Html.Raw(item.ImageUrl)" />
but I agree that using invalid URLs (with spaces) doesn't seem very useful in the long run.
I'm trying to create a news feed for my website. Right now the news column is of type longtext. I've varchar but it didn't work as well. There are a few rows with formatted text. I mean they have "carriage return". I get data from the database in code behind and set them to sessions. In design page I've tried this:
<div>sessioncode.toString()</div>
<div>sessioncode</div>
<div class="panel-body">
<%:Session["HaberIcerik"]%>
</div>
Both ignored \r (carriage return). The <a> tag looks like a normal <a> string. In code behind I did this:
sessionvalue = sqldatas.rows[0]["newscontent"];
sessionvalue = sqldatas.rows[0]["newscontent"].toString();
Session["HaberIcerik"] = haber.Rows[0]["haberIcerik"];
What are your suggestions?
A web browser will convert and condense new-lines to a whitespace as mandated in the HTML specification which requires <br /> be used to indicate a line break.
Either pre-process the string before rendering:
sessionvalue = Regex.Replace(input_string, #"\r\n?|\n", "<br />");
Or render as-is but within an element that has the white-space: pre CSS style.
I have this string below:
<List>\r\n <First>\r\n <Second>BlaBla..</Second>\r\n...
But in View (MVC Asp.Net) presenting in one line only.
What can I do to respect the \r\n to broke in a new line?
Thanks
\r\n will format your string in source, it will be visible when you view HTML source of your page. You need to use HTML <br> tag instead of \r\n, so your browser will format your output accordingly.
Apart from the other answers that suggest using <br/>, you can also use the CSS white-space property which can actually make line breaks when it sees \r\n, or the <pre> tag which has this set up by default. See: http://www.w3schools.com/cssref/pr_text_white-space.asp
Assuming that your view is an HTML page rendered in the browser, understand that HTML does not render line break characters. The source code will, but not the HTML. If you need line breaks, you should use a tag structure that renders them, such as <p> or <br>.
I have a Label which gets from my database the data "1_1" this is the file name of an image I would like to show relative to the ID.
my image link is
<img src="Images/Dropox/.jpg"/>
with no file name
I would like to know how to add a label that on load will be populated with the string "1_1" and then for that string to go into my SRC link.
I have tried adding the label into the space required but that doesn't work.
my idea would be something along the lines of
<img src="Images/Dropox/<asp:Label ID="ImageSequencea" runat="server">.jpg"/></asp:Label>
or something like that. can anyone help?
thanks!
Label corresponds to the <span>. Which means that when ASP.NET engine finds a Label on the page, it takes its Text, wraps into <span>, and sends to the output. Most likely you are not looking for:
<img src="Images/Dropox/<span>1_1</span>.jpg"/>
If you just need url to be Images/Dropox/1_1.jpg with 1_1 being inserted at runtime, you can:
Just turn your img tag into server tag and set its value in code behind:
<img id="DropboxImage" runat="server" />
Use scriplet and call function from code behind
<img src="Images/Dropox/<%= GetImageName() %>.jpg"/>
User asp:Image and set its ImageUrl property in code behind:
<asp:Image runat="server" ID="DropboxImage" />
Try this,
<img src="Images/Dropox/<%#Eval("ReturnEntityName")%>.jpg"/></asp:Label>