8

I looked at this tutorial for creating a javascript that is used withing several projects in the same solution. Basically, I created a class Library where I have a script folder in which I create a script file which I want to use in several projects within the same solution.

In the script folder of the projects which use the commonscript file, I added an existng item as described in the link above. I added the file as a link and not a copy of the file. However, my view s not able to read this javascript file.

I even add this command to the pre-build event for project using the javascript so I could copy the file from the classlibrary to my project during before building but did did not work either.

copy $(SolutionDir)ClassLibraryName\Scripts\CommonScript.js* $(ProjectDir)Scripts\CommonScript.js

and added the script to my view

<script src="@Url.Content("~/Scripts/CommonScript.js")" type="text/javascript"></script>

How can I create a javascript file and use it in several projects in the same solution

3
  • Make it a solution file.
    – abc123
    Jan 27, 2013 at 23:58
  • Added the file in a folder in the solution file, but still does not work. I created a folder in the solution explorer, added the javascript file to this new folder, and added a link to this file in my projects but the projects can still not read from it. They work fine when the file is physically there
    – jpo
    Jan 28, 2013 at 0:22
  • Using a VirtualPathProvider class you can add your Javascript file as an embedded resource and process the path programmatically. I made a blog post about embedding MasterPages as resources a few years ago for old asp.net projects but it is in French (the code is still the same...) The same process should work for script files in an MVC project. If you really need this I can translate it. christopheargento.com/2011/01/17/… Feb 1, 2013 at 21:56

3 Answers 3

1

You might also consider restructuring your application to use areas rather than using two fully separate projects. I've had the most success using multiple projects only for fully separate parts of the application (i.e. interfaces in one project, models in another, class definitions in a third, areas, controllers, and views in a fourth). If you use areas, then your views can pull from the same set of resources (such as Content and Script folders).

2
  • I think to assume creating areas in MVC is to say that each application is functionally similar in nature. This might not be the case. Sharing static assets is the ultimate goal. Creating areas in MVC is like hitting a thumbtack with a sledge hammer in this case. Mar 10, 2016 at 14:38
  • Hence consider, there aren't many cases I could imagine for a web application where the applications are so different that they couldn't be assigned to areas in the same project, but obviously there will be cases where they are. If it is possible in this case though, restructuring in this way would solve the problem.
    – Ceshion
    Mar 10, 2016 at 14:45
1

The other alternative is to have a "media server" or "static asset server" which is separate from your web sites. This could just be another virtual directory in IIS that could contain any static assets, like JavaScript, CSS or images. Then you would need to reference those assets properly from your MVC projects:

<script type="text/javascript" src="/assets/foo.js"></script>

You can still utilize relative URLs if the static asset virtual directory is on the same server. I might suggest this setup for IIS:

  • Default Web Site
    • assets/ (virtual directory)
    • webapp1/ (application)
    • webapp2/ (application)

Since the "assets" virtual directory is just a folder with files, you wouldn't even need to create a Visual Studio project for it. Using a text editor like Sublime Text would suffice, since it can open up a folder and present it like a project.

0

Have you thought of hosting your files? This is something that is generally done in industry, and a habit I get into on my own projects. It's pretty easy to set up with Apache or IIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.