3

I've seen other websites state that it's possible to use a shared project for Javascript, CSS, and other static files. I already have some C# files in my shared project.

I'm able to create the shared project and add the files to it. I can even find these files that are in my shared project in my web application using Intellisense. However, I always get a 404 error whenever I am running my web app locally. It cannot find the files that are in the shared project.

What can I do to fix this?

Thanks!

2
  • Which resource is getting error 404 & where it is referenced & exists? Commented Sep 14, 2018 at 5:06
  • 1
    I have a CSS and a JS file that are getting 404 errors. They are referenced in a view in my web application. <script src="~/Content/Grid.css"></script>. In the shared project Grid.css is inside a Content folder. I have added a reference to the shared project in my web application. Commented Sep 14, 2018 at 12:47

2 Answers 2

1

When you add a file to your project in Visual Studio it defaults to not copy the file to your output directory.

To automatically copy the files to your output directory when you build your project: select the files in Solution Explorer, set the "Build Action" to Content and "Copy to Output Directory" to Copy always or Copy if newer.

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

3 Comments

This makes sense, but it did not work when I tried it. Maybe that was part of the problem but not the whole problem. Thanks
After you build, look in your output directory and check if all the files are in the right paths according to where you are referencing them in your web app.
The JS and CSS files that give me a 404 are not there.
1

I came with a solution to this, the only thing you need to do is to add following code to the file YourSharedProjectName.projitems (which should be in your shared project folder):

<ItemGroup>
  <None
      Include="$(MSBuildThisFileDirectory)Content\**\*.*"
      Link="..\Content\%(RecursiveDir)%(Filename)%(Extension)"
      CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

In this case I wanted to have Content folder shared among different web projects in the solution, so I created "Content" directory in SharedProject and added there directories and files I need (Scripts, Styles, etc.). Then after adding this MSBuild instruction in .projitems file, all the files get copied to the corresponding "Content" directory of each project. This way you can even have some project specific files in its own "Content" as long as there are no identical file names.

2 Comments

new line is your friend
I have to update a bit to copy my wwwroot folder (in Shared Project) to each sharing web apps: <ItemGroup> <None Include="$(MSBuildThisFileDirectory)wwwroot\**\*.*" Link="wwwroot\%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="Always" /> </ItemGroup>. This copying works for publishing but not for F5 to debug. Reading tones of docs about sharing static files via shared project, it seems hard. I finally give it up. Instead I put the shared wwwroot in one project (not shared project), and add absolute site url when using these files. Not elegant but worked.

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.