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

风格不统一 提交于 2020-07-02 08:14:07

问题


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 references to our css and js files, how does it work in ASP.net Core? Let's say that I created a test.js and a MyStyle.css class, which is the best way to add references to it in every view? Should I place the .js and .css files inside wwwroot/js and wwwroot/css?


回答1:


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




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/39839123/how-to-add-js-and-css-files-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!