How to add js and css files in ASP.net Core?
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
How to use css files or js in Area on ASP .NET Core
If the structure of our area is as follows :
You need to Copy these codes to the Configure section in Startup under the app.UseStaticFiles()
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new
PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(),
"Areas\\Foo\\Content")),
RequestPath = new PathString("/Foo/Content")
});
and link the file like below :
<link rel="stylesheet" href="/Foo/Content/bootstrap.min.css" />
How to dynamically add links to CSS and Javascript files in ASP.NET Core
I'm assuming the CSS files that are selected are persisted via a database by your CMS, and rather than having a static list of CSS ids, you're actually pull the info from a database. In that scenario, your best bet is a view component:
public class CustomCssViewComponent : ViewComponent
{
private readonly MyContext _context;
public CustomCssViewComponent(MyContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var ids = // pull in the ids from your context;
return View(ids);
}
}
Now, you're essentially just passing in the ids as the model for the view backing this view component. So, go ahead and create that view at Views/Shared/Components/CustomCss/Default.cshtml
:
@model List<int>
<link href="/css/customCss_@(String.Join("_", Model)).css" rel="stylesheet" />
Finally, in your layout or wherever you want this included:
@await Component.InvokeAsync("CustomCss")
Rinse and repeat with your custom JS. You can probably handle your custom HTML the same way, but you may also consider using custom tag helpers.
Add JavaScript Reference in asp.net core
In ASP.NET Core, static files such as javacript, css etc are serve to web browser from wwwroot
folder.
Hence, you need to move these files into wwwroot
folder and then reference files in _Layout.cshtml
using <script src="~/JavaScript/GlobalJs.js"></script>
For more information, please refer this article
Including Css and Js files programatically in .Net Core Layout View
In ASP.NET Core, we don't use Server.MapPath
. We want to use IHostingEnvironment
instead.
In the file _Layout.cshtml
, you could try:
@using Microsoft.AspNetCore.Hosting
@inject IHostingEnvironment environment
@{
string path = string.Format("js/page/{0}/{1}.js", controllerName, actionName);
}
@if (System.IO.File.Exists(System.IO.Path.Combine(environment.WebRootPath, path)))
{
<!-- file exist here... -->
}
In the controller, you can pass it via constructor:
public class HomeController : Controller
{
private readonly IHostingEnvironment _environment;
public HomeController(IHostingEnvironment environment)
{
_environment = environment;
}
public IActionResult Index()
{
string path = System.IO.Path.Combine(_environment.WebRootPath, "js/page/...");
return View();
}
}
Css and js files are not including into target project when I install my custom Nuget pack
If you nuget package contains css and js file.It is a client-side library.
For client library,when you install the nuget package,it would not shown in your project.It would exist in %UserProfile%\.nuget\package
by default.
Microsoft produce a lightweight, effective solution for web developers to easily manage common client-side library files——Libman.
If your package is not provided by official csdn,you could specify the library's location like below:
- Choose the Provider(filesystem)
- Choose the library's location
- Type the Target location
Click Install button and it would generate the following code in
libman.json
like below:{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": [
{
"provider": "filesystem",
"library": "C:\\Users\\XXX\\XX\\",
"destination": "wwwroot/lib/Chart/Content/"
},
...
]
}
Reference:
https://stackoverflow.com/a/29200530/11398810
https://stackoverflow.com/a/43497854/11398810
Related Topics
Prevent Property from Being Serialized in Web API
How to Get First Record in Each Group Using Linq
Debugging Automatic Properties
Dependent Dll Is Not Getting Copied to the Build Output Folder in Visual Studio
Injecting Dependencies into ASP.NET MVC 3 Action Filters. What's Wrong with This Approach
C# - Code to Order by a Property Using the Property Name as a String
Best Way to Invoke Any Cross-Threaded Code
How to Represent a Time Only Value in .Net
Try/Catch + Using, Right Syntax
Get Transactionscope to Work with Async/Await
How to Use Jwt in MVC Application for Authentication and Authorization
When Do We Need to Set Processstartinfo.Useshellexecute to True
How to Read Connection String in .Net Core
Difference Between Casting and Using the Convert.To() Method
Implicit VS Explicit Interface Implementation
Unitywebrequest Embedding User + Password Data for Http Basic Authentication Not Working on Android