Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 802
Mike Trusov Avatar asked Nov 07 '12 04:11

Mike Trusov


2 Answers

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

like image 120
Mike Trusov Avatar answered Sep 19 '22 11:09

Mike Trusov


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.

like image 36
mikek Avatar answered Sep 19 '22 11:09

mikek