KDT
KDT

Reputation: 671

How to make use of common files across projects using an IIS7 virtual directory

The scenario:

I'm very new to ASP.Net MVC programming and running into a wall constantly trying to make use of common files (.js, .css) across multiple projects. The idea is to have these generic files in 1 location which provides for easy future updates and avoids the "copy and paste" dilemma across all the projects. I've set this folder up in IIS7 as a virtual directory in the default website with an alias "CommonFiles".

The problem:

With MVC-4 I'm trying to add the js files to a script bundle but upon running the application it's not picking the files up at all. (checked in the page source and also added a js function as a test)

Code snippet in BundleConfig.cs:

bundles.Add(new ScriptBundle("~/bundles/test").Include("~/CommonFiles/test.js"));

Rendering in _Layout.cshtml:

@Scripts.Render("~/bundles/test")

I've read quite a few posts ( Script Bundling in WebForms with Virtual Directories (asp webforms though), How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app, ScriptBundle not rendering scripts that are in a VirtualDirectory) but i'm afraid my lack of knowledge on MVC is limiting my path forward and really hoping to get some insight into how MVC handles IIS virtual directories and if it's even an easy possibility given the last post i've read above.

Can this be done in MVC-4 and if not what is a second best alternative in reusing common code across projects?

Upvotes: 0

Views: 706

Answers (1)

KDT
KDT

Reputation: 671

After reading a post by kev (Using ServerManager to create Application within Application) it put me on the right path and the issue I had is actually embarrassing.

For the sake of other devs landing on this post with a similar issue in visual studio, this is what fixed my issue:

Problem:

I make use of a separate project which contains files that are used across multiple other projects. I created a virtual folder in IIS7 referencing these files. This means if a change is needed to the common files, it's updated once and all the other projects will automatically "see" the change.

My other individual projects make use of script bundling to include files relevant only to the said project, but also to reference the common files in the virtual folder as defined in IIS.

My MVC-4 web application wasn't picking up the common files given the syntax above, in neither debug or release..

Solution:

When developing in VS2012, under the project's properties, there's a setting under the web tab where you can specify whether you want to use local IIS web server or IIS Express to test your application. IIS Express adds a random port to the site in order to test, and to allow multiple instances of sites to run (on different ports). This seems to throw the virtual directory include off in the bundling.

Using IIS Express

Choosing to use the local IIS server is closer to what the "live" environment would be in my opinion. Just un-tick the "Use IIS Express" setting.

enter image description here

As a side note and for more info on what the difference between the usage of IIS and IIS express is and whether it's suitable for your environment (as it was for mine) see this link:

http://msdn.microsoft.com/en-us/library/58wxa9w5.aspx

Hope this helps someone in future and saves them the amount of time I wasted on this!

Upvotes: 1

Related Questions