Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static files in blazor hosted project

It is a pretty simple Task, I want to use images in my razor pages which are located in wwwroot/css/images and use it in html like this.

<img src="~/css/images/logo.png"/>

In a normal ASP.Net Core application I would just add app.UseStaticFiles() in the Configure method, but since this is a blazor hosted application, it changes nothing.

I also tried adding the app.UseStaticFiles() in the server project, but with no success either.

If you want to try it out yourself just create a blazor-hosted project from the templates and try to serve/display images like I did.

like image 586
Twenty Avatar asked Mar 04 '23 17:03

Twenty


1 Answers

Blazor serves static file just fine. The issue you’re having is the syntax you’re using to reference the file. You just need to drop the ~ symbol.

<img src="/css/images/logo.png" />
like image 200
Chris Sainty Avatar answered Mar 28 '23 08:03

Chris Sainty