Mike Trusov
Mike Trusov

Reputation: 2018

Is there a way to change .net mvc bin dir location?

I have a fairly standard and simple MVC4 website.

In root dir we have: bin, content, scripts, views. Using default settings the project's DLL, let's call it "web.dll" and all the necessary extras go in the bin dir.

Somehow, ASP.NET dev server and IIS7.5 both know to look for "web.dll" in the bin folder when hosting the site, and if it's not there they throw error: "Could not load type 'CVD.Web.MvcApplication'". The standard solution for that error is to build directly into bin folder, which doesn't work for me because...

For debugging purposes, I'd like to be able to build Debug & Release configurations into into bin/Debug and bin/Release respectively and then deploy both dirs, then change a setting either in IIS, web.config, global.asax, or anywhere else to pick whether Debug or Release build should be loaded and executed by the server.

I haven't been able to find if that's possible or if .net webapps have a silly hard-coded rule saying all code must live in the bin dir.

Upvotes: 17

Views: 9060

Answers (3)

user1593842
user1593842

Reputation: 357

Since probing privatePath is very limited (it can only point to a sub-directory) I think mikek answer is the best one. But there is still one alternative: manually edit the applicationhost.config file and change the path.

Inside the applicationhost.config file (which should be located inside the dir .vs\config\ which Visual Studio creates inside your solution dir), find your site and change the physicalPath to the anyplace you want.

Just remember that you will have to set the path to one dir above the actual bin dir. And it gets a little bit worse: you will also have to remember to copy both the Global.asax and the web.config to that dir. In the end you will have

phsicalPath="Z:\myRamDisk\MySite"
    Z:\myRamDisk\MySite
        bin\
        Global.asax
        Web.config

You can use a build even to copy the required files.

It sucks, but at least it works. It is either that or the mklink suggestion by mikek.

Upvotes: 0

mikek
mikek

Reputation: 81

I had the same issue. To solve it, I created a directory junction called "bin" to the target directory in the Post-Build Event:

if exist "$(ProjectDir)bin" rmdir "$(ProjectDir)bin"
mklink /J "$(ProjectDir)bin" "$(TargetDir)"

This way you can set your Output Path to somewhere else, even completely outside the source tree.

IISExpress will still run from your project folder in the source tree, but follows the bin junction to find the built DLLs. Note that the other files will be used from the source tree location.

The junction is removed and recreated each time, in case you switch build configurations.

Make sure the bin directory doesn't already exist from your old build setup, the rmdir won't work if there are files in the (real) bin directory.

Upvotes: 5

Mike Trusov
Mike Trusov

Reputation: 2018

OK, so with the help of few links provided by Sen Jacob and some more research I've figured out that it is possible to do it all just using web.config.

First of all we need to provide the new path and tell the assembly name since we're steering away from defaults:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="Something.Web" />
            </assemblies>        
        </compilation>
    </system.web>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <probing privatePath="bin\debug" />
        </assemblyBinding>
    </runtime>
</configuration>

Now, for some reason everywhere else people suggest specifying privatePath relative to bin dir (i.e. privatePath="debug"), however in my case it had to be relative to app root (i.e. as above). Maybe it's a change in .net4 or some other configuration setting I'm missing, not sure; if someone has a better idea feel free to edit/comment.

At this stage, if the server finds the file, and successfully loads the assembly and the class it will start complaining about all the missing referenced files, which I had to add right next to <add assembly="Something.Web" />:

    <assemblies>
        <add assembly="Something.Web" />
        <add assembly="System.Web.Mvc" />
        <add assembly="System.Web.Optimization" />
        <add assembly="System.Web.Helpers" />
        <add assembly="System.Web.WebPages" />
    </assemblies>  

From what I've gathered it re-compiles the assemblies on site startup (not sure).

Sources: 1 2 3

Upvotes: 11

Related Questions