How to add js and css files in ASP.net Core?

前端 未结 5 1130
日久生厌
日久生厌 2020-12-29 23:44

I\'ve been assigned to migrate an application from MVC into ASP.net Core, I\'m new to ASP.net Core. In MVC we have BundleConfig.cs and in there we add reference

相关标签:
5条回答
  • 2020-12-29 23:55

    I put the JS and CSS files in an assets folder in the wwwroot folder of my application and then used link tags in my _Layout.cshtml to bring in the styles. Steven Sanderson has part a of a blog post where he talks about adding third party libraries here.

    You could also do some sort of setup using webpack. I admit, this topic is not very straight forward.

    0 讨论(0)
  • 2020-12-29 23:56

    In _layout:

    @await RenderSectionAsync("Styles", required: false)
    

    Then your view:

    @section Styles{ your CSS }
    

    Information from: https://dev.to/amjadmh73/loading-custom-css-files-in-razor-pages-4no9

    0 讨论(0)
  • 2020-12-29 23:57

    It might be a bit late to answer the question, but for the ones who come here via Google: I found that adding asp-append-version="true" worked for me, as all the other options given in the other answers were already provided in _Layout.cshtml.

    0 讨论(0)
  • 2020-12-29 23:58

    I added the references inside the _Layout view, inside the <environment> tags:

    <environment names="Development">
      <link rel="stylesheet" href="~/css/MyCss.css" />
    </environment>
    

    If anyone knows a better way I would welcome it

    0 讨论(0)
  • 2020-12-30 00:08

    The better way to do it is to use: app.UseStaticFiles(); inside startup.cs before routing.

    0 讨论(0)
提交回复
热议问题