CSS doesn't work in ASP.NET Core Razor Pages

Shervan360 1,521 Reputation points
2023-09-03T13:52:35.0066667+00:00

Hello,

CSS doesn't work in ASP.NET Core Razor Pages. Could you please help me?

I attached the sample project in OneDrive. (here)

namespace WebApplication6
{
     public class Program
     {
          public static void Main(string[] args)
          {
               var builder = WebApplication.CreateBuilder(args);
               builder.Services.AddRazorPages();

               var app = builder.Build();

               if (!app.Environment.IsDevelopment())
               {
                    app.UseExceptionHandler("/Error");
                    app.UseHsts();
               }

   
               app.UseHttpsRedirection();
               app.UseStaticFiles();
               app.UseRouting();
               app.UseAuthentication();
               app.MapRazorPages();

               app.Run();
          }
     }
}
@page
@model WebApplication6.Pages.ToQueryStringModel
@{
}
<h3 class="test">Binding!</h3>
<div class="card-grid">
     <div class="caption">Title</div>
     <div class="value">data</div>
     <div class="caption">Title</div>
     <div class="value">data</div>
</div>
@{      Layout = "_Layout"; }
@using WebApplication6
@namespace WebApplication6.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,510 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Shervan360 1,521 Reputation points
    2023-09-05T12:42:27.38+00:00

    I found the problem. the name of _ViewStartS.cshtml was wrong. _ViewStart.cshtml is true.

    1 person found this answer helpful.
    0 comments No comments

  2. tebh 0 Reputation points
    2023-09-03T15:10:08.82+00:00

    Hi there, I see that in your razor page, there's nowhere where you reference the styles..


    Above the general block

    @{
    
    }
    

    You need to have something like this:

    @section Styles {
    	<link rel="stylesheet" href="[~/location_of_your_css_file]" />
    }
    

  3. Karen Payne MVP 35,401 Reputation points
    2023-09-03T19:02:44.08+00:00

    Here are some ways to provide styles

    • Add the styles in wwwroot/css/site into a .css file which is global to all pages
    • Add a style sheet file under wwwroot/lib and add a link tag to Pages/Shared/_Layout.cshtml
    • Use CSS isolation as in my article with code sample.
    • Place a link tag at the top or bottom of the desired page for the style sheet file.
    • See Microsoft docs
    • Lastly, you can check if a style sheet is loaded in any web browser developers tools.
    0 comments No comments

  4. SurferOnWww 2,661 Reputation points
    2023-09-04T00:39:04.8066667+00:00

    CSS should work if it is properly included in the page. if you say "CSS doesn't work." It probably means that CSS in not properly included in the page. Therefore, I suggest that you use the developer tool of browser to see if CSS is included.

    css


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.