I am studying a book on ASP.Net MVC 4. The project created by the VS environment in the book is a little different from mine because it is outdated. According to the book's instructions, I have to add a style sheet by adding a line:

<link rel="stylesheet" type="text/css" href="~/Content/Site.css" /> 

But having tried all the options known to me, the table remains invisible.

 @model WebApplication1.Models.FirstModel @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>RsvpForm</title> <link rel="stylesheet" type="text/css" href="~/Content/Site.css" /> <link rel="stylesheet" type="text/css" href=@Href("~/Content/Site.css") /> <link rel="stylesheet" type="text/css" href=@Url.Content("~/Content/Site.css") /> </head> <body> @using (Html.BeginForm()) { @Html.ValidationSummary() <p>Your name: @Html.TextBoxFor(x => x.Name) </p> <p>Your email: @Html.TextBoxFor(x => x.Email)</p> <p>Your phone: @Html.TextBoxFor(x => x.Phone)</p> <p> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[] { new SelectListItem() {Text = "Yes, I'll be there", Value = bool.TrueString}, new SelectListItem() {Text = "No, I can't come", Value = bool.FalseString} }, "Choose an option") </p> <input type="submit" value="Submit RSVP" /> } </body> </html> 

This is what a browser looks like:

enter image description here

This is what the browser window looks like:

enter image description here

  • And if in the browser to open localhost: 65516 / Content / Site.css what happens? - Sergey Glazirin
  • @SergeyGlazirin Page not found - lalala lala
  • As an option, if you are doing in Visual Studio, you can drag your file with the mouse via drag'n'drop to the page from Solution Explorer (Обозреватель решений) , the path seems to be automatically entered. If the path is the same, then it will be necessary to think, if not, then most likely it will work. - Sergey Glazirin
  • @SergeyGlazirin Dropped as said, but nothing has changed = ( - lalala lala
  • one
    You can look here - WebMorda

2 answers 2

In the Startup.cs file you need to connect statics:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(); } 

And, ASP.NET MVC 4 is required, not immediately noticed. But most likely there is the same.

  • The Configure method already exists in the Startup file. In addition to the method "app.UseStaticFiles ();" there it is called: app.UseMvc (routes => {routes.MapRoute (name: "default", template: "{controller = Home} / {action = Index} / {id?}")}}; Without which the page in the browser is not displayed at all. - lalala lala

By default, static files are stored in the wwwroot folder.

If you want to specify your own folder, you must override the path in the Program.cs file. To do this, you need to add .UseWebRoot("Content") .

It looks like this:

 public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseWebRoot("Content") .Build(); } 

After that, you can access static files, but now they will be taken from the Content folder, not from wwwroot.