Blazor Web Assembly - accessing images in RCL - image

I'm new to Blazor - so this may be obvious but I can't figure it out. I have a WebAssembly project and a separate Razor Class Library where I have some components. I have a component that I want an image in - I place the image in the wwwroot/img folder of the library and access as below
<img src="/img/logo.png" alt="test logo" />
But it will not display - I've tried
<img src="_content/img/logo.png" alt="test logo" />
But it won't display either.
The only way I can get my image to display is to put it into the wwwroot/img folder of the Client Project. But I want my component library to be able to be used in other projects.
Any guidance much appreciated. I'm coming from a Windows Forms background so I have a lot to learn :)

I´m also new to web development, but afaik the wwwroot folder serves as folder for static files your webpage uses (this is also the only folder the browser can access for media files for security reasons as I have understood it).
I had the same problem some days ago, I solved it by putting everything into the wwwroot folder, but I also found an article in which another way is suggested.
http://blog.vivensas.com/static-image-in-blazor-inside-and-outside-web-root/
There it is basically suggested to create a method in the Startup.cs file, which will be able to get your images from elsewhere:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
app.UseStaticFiles( new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path. Combine(Directory . GetCurrentDirectory(), “StaticFilesFolder” )),
RequestPath = “/StaticFiles”
});
}
Then you can use your images like this:
#page “/”
<h3> Display Image in Blazor</h3>
<div>
<img src=”/StaticFiles/Image/OutsideWebRoot.png” />
</div>
#code {
}
Edit: Maybe I misunderstood your question, for this to work it also has to be in the client project. I don´t know the way to get images from other solutions than the client project.

I came across this problem as well in my Blazor Server application (.NET 6). I was using RCL in my blazor application and when I tried to save static assets in my RCL's /wwwroot/images and make use of them in that same RCL's component (to show an icon), I got a 404 when I utilized that in my Blazor app.
Here's how I did a little trick to get past this problem:
I stored all my static assets in my Blazor app (instead of RCL)
In my Blazor app's styleseet (site.css or any other), I created CSS variables for those assets, like:
--logo: url(/images/logo/logo-light.png);
and then in my RCL, I made use of them using a class selector and applied the content to that CSS variable created, like:
.icon:before {
content: var(--logo)
}
Worked like a charm.

2 years later, just in case... at least in .Net 5, 6,...
To fix the problem you need to set the source of the image tag to _content/YourRazorClassLibraryName/img/logo.png
Example:
<img src="_content/YourRazorClassLibraryName/img/logo.png" />
Important
If you do not reference the image from HTML, like in the example, the image will NOT get downloaded to _content/YourRazorClassLibraryName/img. So, at least one html element in your app needs to use the image, otherwise it will not get downloaded. Setting the background image of elements in CSS will also cause the image to get downloaded.
You can inspect this by:
running your app
pressing F12 in chrome\edge to launch developer tools
go to Sources tab
expand [site_url]/_content/[YourRazorClassLibrary]
check if img folder is there or the image that is not referenced in the HTML exists

Related

Maintaining image preview in Netlify CMS

When uploading an image in Netlify CMS, I get a preview image, which however disappears right after a refresh. The image I attached shows the source attribute the preview element uses - it looks like the logic here assumes that the image is already deployed to the same domain (which in turn assumes that the site the CMS manages lives on the same domain as the CMS interface itself). Is there a way to change the base URL just for the preview, e.g. to some kind of https://rawgit/...-like URL. I am looking for a setting that affects only the preview widget behavior and nothing else (where the image is saved, what URL path it adds in the post frontmatter etc).
Thanks in advance for any pointers!

CSS and images do not appear until my application makes a database call

I'm hoping this isn't a double-post - I couldn't find my issue in the search, but I imagine it might be asked in several ways. My project involves a PHP5 workspace running the Laravel 4 framework.
Quickly, my issue occurs when I access any webpage within my private project that does not first make a call to a database. When viewing the page, my images are missing and the CSS styles - though correctly called in source, and accessible when I click on the HREF's for my styles - aren't displayed properly.
After going to a page that makes a database call (for instance, my Manage Users page which displays a list of users in the database), all images and styles in all pages work. Essentially, I:
Access the homepage, which is static HTML. It's styles are broken.
Access an admin panel page which is static HTML. It's styles are broken.
Access an admin panel page that pulls a list of users to the database. The styles suddenly work.
Any pages I visit after this point display styles properly.
Has this happened to anyone else? Is there a simple way to resolve this?

Making content accessible on Addon SDK

I am developing an addon using Firefox's Addon SDK (v. 1.11). My extension dynamically creates an iframe on each website and then loads an html file which includes other resources such as images, font files, etc. from the add on's local directory.
Problem
When loading any of such local resources (i.e.: "resource://" schema), the iframe fails to display them and a message is thrown:
Security Error: Content at http: //www.XXX may not load or link to
resource://XXX
This is a security measure introduced on Firefox 3. When developing without the Addon SDK, the way around it is declaring a directory with "contentaccessible=yes", making the directory's contents accessible to anyone, including my add on. However, I have not been able to find similar functionality using the Addon SDK. Is there a better way of using local data on an iframe that my addon creates and inserts into a page?
I don't think you can directly load an iFrame that points to a resource inside your URL. The browser complains because it's either breaking same origin policy or cross site scripting one. I can't remember which one right now.
if it is html content you want to load you can always inject it into the DOM and then send a message to the document object using the events API to display your custom html. I've done this in the past and it works.
so from main.js send a message to content script which will then inject your iframe html into the DOM and then you can send the document object a message to display it.
I hope this helps.
Not sure if this was the case when you posted the question, but it appears that "resource://" should no longer be used with the Addon SDK.
If you're using the resource inside of an HTML file in the extension, you can reference it locally, otherwise you should use data.url('whatever.jpg') and pass around that value as needed.
Full info is here: http://blog.mozilla.org/addons/2012/01/11/sdk-1-4-known-issue-with-hard-coding-resource-uris/

Multiple Viewports in Extjs

I have 100 different url's each pointing to plain html pages, I have to convert these plain html pages to extjs pages. Footer and Header is same for all the pages but center section is different for different plain html pages.
In order to leverage maximum code I thought to create an EXTJS4.1.0 application which will have model, view, store and controller but separate index.html, app.js and viewport.js. The reason for separate index.html, app.js and viewport.js is that if I don't keep single index.html, app.js and viewport.js then I will not be able to bookmark the pages (Since users and using 100 different urls for 100 different pages) so basically I will have 100 different app.js and corresponding viewport.js (viewport.js will have common components for footer/header etc and specific components for screen) but at one point of time only one index.html, app.js and viewport.js will be active/run.
My first question is, is the solution I just mentioned a correct solution.
Is it true that in Extjs MVC pattern you can only have one Viewport.js file per application if you use autoCreateViewport: true.
Secondly if you use autoCreateViewport: false in Ext.Application (in app.js ) and create instance of different Viweport in launch method as follows:
Ext.application({
name: 'Panda',
autoCreateViewport: false,
launch: function() {
Ext.create('Panda.viewport.ViewportOne')
}
});
Notice that I have created instance of ViewportOne which is one of 100 viewports. With this solution also I ran into glitches while running the application (some navigation will not work etc). Is it because extjs does not allow viewport with name such as Panda.viewport.ViewportOne, Panda.viewport.ViewportTwo etc.
How in Extjs I can create fully MVC style application with bookmarkable pages such that each page is part of same extjs application (ie it leverages same model view, store, controller and dynamic loading).
The company I work for has lots of customers who are using an application which was build on plain html and javascript. Each uses works on only one page of that application which he has bookmarked on his browser.
The task I am given is to upgrade those plain html/javascript pages to Extjs one such that uses dont have to update their bookmarks.
At my end I am thinking to create single application in Extjs 4.1.0 which will have 100 pages (index.html, indexOne.html etc which user will bookmark) each if which can use common Model/view/store and controller.
So basically my entry point to application will be different but all pages will be using same model/view/store/controller.
Its more of an architectural question.
Hope this help.

Why won't my styles show in a Dynamics CRM 4 IFRAME?

I have created a web page (ASP.NET) that includes a stylesheet to mimic Dynamics CRM styles. This is to be used in a CRM IFRAME (within a form).
The stylesheet is referenced as follows:
<head id="Head1" runat="server">
<link href="Styles.css" rel="stylesheet" type="text/css" />
</head>
When I load the page in Visual Studio, all is well. When I load it in CRM, none of the styles are shown and no images are displayed. If I browse directly to the image, I get a 404 error. However, the pages function correctly.
I have set read permissions for "Everyone" on the server to see if that was causing a problem but it didn't help. I also tried putting a plain HTML page in the folder and that won't load either - again a 404.
The page is installed in the ISV folder ..../isv/MyProject.
Can anyone help?
EDIT
This is on a multi-tenancy system. On my test company (testcompany) if I browse to http://crm/testcompany/isv/MyProject/MyPage.aspx, the page is loaded (without styles and images).
If I browse to http://crm/testcompany/isv/MyProject/TestImage.gif, the image is not shown.
If I browse to http://crm/isv/MyProject/TestImage.gif, the image is shown. Does this suggest a problem with the server setup and the way CRM messes around with virtual directories?
This does mean that if I set the styles reference to /isv.MyProject/Styles.css, the page looks correct. However, as I don't know where the project will be installed by customers, this is inappropriate.
The issue is that CRM uses an ASP.NET Virtual Path Provider to include the tenant name in the URL path. But for file types that are not handled by the ASP.NET handler, IIS fails to find them.
As a work around, you can reference your CSS from your HTML page using a path like href="../Styles.css"
Actually you'd probably be better off using href="/ISV/MyProject/Styles.css" because when CRM is accessed using an Internet Facing Deploy configuration, it does not include the tenant name in the path.
Although already answered, this post describes the 'problem' in detail http://blogs.msdn.com/emeadcrmsupport/archive/2010/03/22/how-to-create-a-relative-url-in-site-map-to-static-content-under-the-isv-folder-in-crm-4.aspx

Resources