0

I am following one tutorial and CSS worked for Shared razor pages. First example works fine and does everything it needs, but the second does not load CSS. No color changes or image size.

@inject IProductService ProductService
@if(ProductService.Products == null || ProductService.Products.Count == 0)
{
    <span>Loading products...</span>
}
else
{
    <ul class="list-unstyled">
        @foreach (var product in ProductService.Products)
        {
            <li class="media my-3">
                <div class="media-image-wrapper mr-2">
                    <a href="/product/@product.Id">
                        <img class="media-img" src="@product.ImageUrl" alt="@product.Title">
                    </a>
                </div>
                <div class="media-body">
                    <a href="#">
                        <h4 class="mb-0">@product.Title</h4>
                    </a>
                    <p>@product.Description</p>
                    <h5 class="price">$@product.Price</h5>
                </div>
            </li>
        }
    </ul>
}
@code {
    protected override async Task OnInitializedAsync()
    {
        await ProductService.GetProducts();
    }
}
 

CSS FILE

    .media-img{
    max-height: 200px;
    max-width: 200px;
    border-radius: 6px;
    margin-bottom: 10px;
    transition: transform .2s;
}
.media-img:hover{
    transform: scale(1.1);
}

.media{
    display:flex;
    align-items: flex-start;
}
.media-body{
    flex: 1;
    margin-left: 20px;
}

media-img-wrapper{
    width:  200px;
    text-align: center;
}


@media(max-width: 1023.98px)
{
    .media{
        flex-direction: column;
    }
    media-img-wrapper{
        width: 100%;
        height: auto;
    }
}
    

But when I try to do the same in the Pages folder

@page "/product/{id:int}"
@inject IProductService ProductService

@if(product == null)
{
    <span>Message</span>
}
else
{
    <div class="media">
        <div class="media-img-wrapper mr-2">
            <img class="media-img" src="@product.ImageUrl" alt="@product.Title">
        </div>
        <div class="media-body">
            <h2 class="mb-0">@product.Title</h2>
            <p>@product.Description</p>
            <h4 class="price">
                $@product.Price
            </h4>
        </div>
    </div>
}


@code {
    private Product? product = null;
    private string Message = string.Empty;

    [Parameter]
    public int Id { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        Message = "Loading Product...";

        var result = await ProductService.GetProductAsync(Id);
        if(!result.Success)
        {
            Message = result.Message;
        }
        else
        {
            product = result.Data;
        }
    }
}
}
    

I added another CSS file and even tried to add wwwroot/css/app.css

.media-img{
max-height: 500px;
max-width: 500px;
border-radius: 6px;
margin: 0  10px 10px 10px;
}

.media-img-wrapper {
    max-width: 500px;
    max-height: 500px;
    text-align: center;
}

@media(max-width:  1023.98px)
{
    .media-img {
        max-width: 300px;
    }
    .media-img-wrapper{
        width:100%;
        height: auto;
    }
}

It does not work. The only difference I can spot is that On pages folder files have a green + sign, while in Shared they have a red V. Files difference

kanils_
  • 385
  • 1
  • 17
  • + sign on file in VS means that file was created – kanils_ Oct 25 '22 at 08:27
  • Also in your css files dont see any code that will change color. Also maybe img is already of the size that you provide. Could you please add some screens with result of execution of 1 code and another? – kanils_ Oct 25 '22 at 08:33
  • I added color change in app.css, and the images are native size, one is small another is takes more than the full screen. What do you mean by result of execution? – Modestas Vacerskas Oct 25 '22 at 08:38
  • I meant produced html in both cases. But I think it's unnecessary. Did you link your css? – kanils_ Oct 25 '22 at 08:42
  • No, in the tutorial it did not show any linking, but it worked for him. And I didn't link anything. But in shared pages it worked – Modestas Vacerskas Oct 25 '22 at 08:44
  • You are using bazor webassembly template? If yes, such css are already linked in Pages/_Layout.cshtml. In your case I didnt see such file, maybe it's linked somewhere else? – kanils_ Oct 25 '22 at 08:46
  • Yes, I used Blazor WebAssemblyApp and ticked ASP.NET Core hosted option it created 3 projects. Webasmbly.client , .server and .Shared – Modestas Vacerskas Oct 25 '22 at 08:53
  • https://stackoverflow.com/questions/5021552/how-to-reference-a-css-file-on-a-razor-view Maybe this could be useful – kanils_ Oct 25 '22 at 08:58
  • Something strange is happening with blazor. I restarted MS Studio and CSS started working on razor.css files but stoped including CSS data from app.css. Every time i change something it requires restarting ms studio. – Modestas Vacerskas Oct 25 '22 at 09:05

0 Answers0