Click here to Skip to main content
15,883,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am looking for a way to reference javascript and css files from a library project in C# mvc. I've found enough references about this where it's handled using static files, but I couldn't get that to work. So I have two projects in visual studio, one is a library and the other project needs to use different files (js, css) from wwwroot library. I can only call views from the library using EmbeddedFileProvider so far. Maybe it would be good to find a complete example with code in visual studio, but so far I haven't found anything like that.

What I have tried:

Startup file, method Configure:

app.UseStaticFiles(new StaticFileOptions
			{
				FileProvider = new EmbeddedFileProvider(
				assembly: Assembly.GetAssembly(typeof("AnyClass")),
				baseNamespace: "Namespace")
			});


Cshtml view:
<link href="@Url.Content("_content/LibName/css/styl.css")" rel="stylesheet" type="text/css" />


or

<link href="@Url.Content("~/css/styl.css")" rel="stylesheet" type="text/css" />
Posted
Updated 20-Sep-21 8:16am

You can apparently do it linking to the files as shortcuts, as explained here, asp.net mvc - How do you share scripts among multiple projects in one solution? - Stack Overflow[^].

However, I have never seen anyone do what you want to do and I would suggest you copy the files into each project that needs them.
 
Share this answer
 
Comments
KarNullKa 20-Sep-21 11:39am    
Copying files is of course a solution, but at least outside the mvc site, the library is something that contains shared data and other projects use that data. I'm just getting started with mvc, but even here there are reusable objects like partial views, or views components, so I thought it would be best to make a library for them. I'd also like to avoid having to maintain all the copies afterwards.

I've seen this link and many others, but I've never followed it, or it's not what I'm looking for. Often the problem is also that the link is older (almost 10 years here, for example) and a lot has changed since then. The link is only about sharing scripts, I would like to at least view components, scripts and css styles.
Member 15329613 20-Sep-21 11:58am    
Scripts and css and images will all be the same. Your views models can be in a project that gets shared. Although when I did that I ran into problems and it just wasn't worth putting the models outside of the main project.
I finally found a functional path:
<link rel="stylesheet" href="~/wwwroot/css/style.css" asp-append-version="true" />

Additionally css file marked as embedded resource and FileProvider as above.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900