3

I am trying to explore MVC6 application. I selected Framework 4.6 and Empty ASP.Net preview template. Using Visual studio 2015.

I have .css file under wwwroot/css directory.

And trying to use in index.cshtml as

<link href="../../css/css.css" rel="stylesheet" />

also tried

<link href="~/css/css.css" rel="stylesheet" />

But it's not working. Is here any technique required?

2
  • In my MVC 6 app <link href="~/css/css.css" rel="stylesheet" /> is working fine. But I have it in the _Layout.cshtml file, I think it should work. Commented Nov 5, 2015 at 11:58
  • Yes it should work, If you use the default starter web template this reference is also used in the _layout.cshtml. Commented Nov 5, 2015 at 18:14

4 Answers 4

7

You have to tell ASPNET5 to use static files. In startup.cs add

app.UseStaticFiles();

in the Configure() function

Sign up to request clarification or add additional context in comments.

Comments

1

try this

  @section scripts{
       <link href="~/css/css.css" rel="stylesheet" asp-file-version="true" />
    }

but recommended it is to add the files to the layout

1 Comment

Welcome to Stack Overflow! I would like to refer you to stackoverflow.com/help/how-to-answer for answering questions. It is important to note your answer could be around for a long time with many different people viewing your answer and as such please try and provide the reasoning for your answer.
1

You need to add your files on the layout page, in MVC 6 you can specify which file you want to use for development, staging or production environment depending where you are working on. Example:

<environment names="Development">
  <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
  <link rel="stylesheet" href="~/css/site.css" />
  <link rel="stylesheet" href="~/css/YourStyleFile.css" />
</environment>
<environment names="Staging,Production">
  <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>

Comments

-2

go to your App_Start Folder > BundleConfig.Cs > then find the code block that will look similar to this

 bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                           "~/Content/themes/base/jquery.ui.core.css",
                           "~/Content/themes/base/jquery.ui.resizable.css",
                           "~/Content/themes/base/jquery.ui.selectable.css",
                           "~/Content/themes/base/jquery.ui.accordion.css",
                           "~/Content/themes/base/jquery.ui.autocomplete.css",
                           "~/Content/themes/base/jquery.ui.button.css",
                           "~/Content/themes/base/jquery.ui.dialog.css",
                           "~/Content/themes/base/jquery.ui.slider.css",
                           "~/Content/themes/base/jquery.ui.tabs.css",
                           "~/Content/themes/base/jquery.ui.datepicker.css",
                           "~/Content/themes/base/jquery.ui.progressbar.css",
                           "~/Content/themes/base/jquery.ui.theme.css"));

then add the path of your css file in here

edit 1

Now you have advised that you are using an empty preview template you have two options,

  1. Add the App_start folder and add the above code block and then in your Layout.cshtml file add the line

    @Styles.Render("~/Content/themes/base/css")
    

or

  1. in your Layout.cshtml file reference is as you would normally, but youll need it in your shared view so it carries through the rest of the site

2 Comments

there is no App_Start folder.
@SimonPrice OP is using Asp.Net 5, you are using a previous version. The project layout has changed significantly. There is no App_Start folder any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.