Asp.Net MVC4 C# Razor Template for Mono on Mac and Linux

Is available on www.github.com/chrisfcarroll/AspNetTemplatesForMono/Mvc4CSharpRazorFx45Intranet. (The github download includes all the required dlls in case you don't have NuGet working, so it's an oversized download. If you do have NuGet working, you'll find everything as NuGet expects).

One step to get the MVC4 template working on Mono

Download from github and open the solution in Xamarin Studio. It should almost work out-of-the-box; the one thing you have to do is either do this from the command line:

sudo mkdir /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
sudo chmod g+rwx /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry

(replacing 3.2.5 with your mono version, which you get at the command line with mono --version);
Or, the alternative to this is to delete the reference to Microsoft.Web.Infrastructure.dll from the project and delete it from the bin directory too.
Small disclaimer: tested on Mac only. If you can offer feedback for Linux/Windows that would be great.

Footnote: How to build the Visual Studio Asp.Net MVC 4 template on Mono

For the interested who have a copy of VS2012, here's the recipe.

  1. From Visual Studio 2012 create a new Project with the C#-Web-MVC4 Wizard and choose Intranet-Razor-No Unit Test.
  2. Try to run the resulting project on Mono on Mac.
  3. Address the problems arising in the following order
    1. System.UnauthorizedAccessException

      Access to the path "/Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry" is denied. (Where the whole path varies by O/S & 3.2.5 is your mono version).

      Resolution

      sudo mkdir /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry
      sudo chmod g+rwx /Library/Frameworks/Mono.framework/Versions/3.2.5/etc/mono/registry

      i.e. allow members of admin group to read/write the registry. Obviously you could widen privileges but this is all that's needed for asp.net.

    2. System.TypeLoadException

      Could not load type 'System.Data.Entity.Migrations.Sql.SqlCeMigrationSqlGenerator' from assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

      Resolution

      Remove references to Entity framework from THREE places: web.config, Project references AND delete them from the bin directory if they in there:

      • System.Data.Entity
      • System.Web.Entity
      • Entity.Framework
    3. System.TypeLoadException

      Could not load type 'System.Net.Http.MultipartRelatedStreamProvider' from assembly 'System.Net.Http.Formatting ...

      Resolution

      add this redirect to the web.config. You should find there are other 'dependentAssembly' redirects in there within the section so put it alongside them:

      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
          <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
        </dependentAssembly>
    4. And finally ... System.Web.HttpException

      Unauthorized.
      Because you don't have windows authentication available to you when running on Mac or Linux.

      Resolution

      Comment out

      <deny users="?"></deny>

      from the web.config.
      Note that you're now unsecured. The simplest way to add security back in is to use Forms authentication. If you don't know how to do this, the simplest start might to look at http://google.com/search?q=Forms+authentication+user+names+in+web.config

Kudos

Asp.Net MVC 4 on Mono and Windows

Having at last got an Asp.Net Mvc 4 project template working on the Mac/Xamarin/Mono, I came to grief again when deploying it to a Windows hosted server. The problem is that whereas mono works without Microsoft.Web.Infrastructure.dll (and indeed is likely to give errors if you have it), for hosting on Windows you must re-include it.
So my deploy process now includes putting Microsoft.Web.Infrastructure.dll back in the web application bin/ directory; and now I can develop on the Mac and deploy to a Windows Server.

Lost SQL Server sa password ? How to start up and login in single user mode

The problem: Someone has lost the sa admin password for your MS Sql Server; or the one person who has SQL admin rights has left the company. Alas, you find that even having Windows admin rights does not grant you access because you have a recent version of Sql Server and you didn't grant Sql Server admin rights to the machine or domain admins.

You can still fix this. You will need local admin right on the machine, and the ability to:

  • open a command line as an administrator
  • look through the registry with RegEdit to find the settings for the version and instance of Sql Server you are locked out of. MSDN mssqlserverloginmode-registry-key has some clues.
  • look through Program Files\Microsoft SQL Server\ and find the binn directory for your version and instance of Sql Server.

The trick is to start Sql Server in single user mode, and then login as a local admin. This will give you admin access to the SQl Server.

How to Get Admin Access to Sql Server on Your Machine

  1. Stop the sql service.
    • This is most easily done via the Windows Services Gui, but net stop MSSQLSERVER might do it. If you have a named instance use net stop MSSQL$instancename
  2. Work out the file location and registry key for the version/instance name of sql server you are trying to get into. This may be trickier than you think - you may have SqlExpress as well as more than one version and instance name of MSSQLServer. For instance:
    • C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn and
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQLServer\

    or

    • C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn and
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.SQLEXPRESS
  3. Change the registry entry for loginmode to 2 (not 0 or 1), which enables both Windows and SQL authentication.
  4. Open a command line window as administrator and navigate to the binn directory you found earlier under C:\Program Files\Microsoft SQL Server\. Run sql from the command line using the –f parameter, sqlservr.exe -f
    • You may need more command line parameters to get your instance running properly, although I never have so far. If so, use the Windows Services Gui to see what the rest of your command line has to be.
    • For a named instance, your command line is sqlserver.exe -f -s instancename
    • An alternative to -f is -m, but -f worked for me.
  5. Open another commandline, also as administrator, and run sqlcmd –S <servername>. Sqlcmd is usually on the path, but if not it should be in the same directory as sqlservr.exe.
    • The server name for local machine is of course '.', as in sqlcmd -S .
  6. Now you can type T-SQL commands. Try Select @@ServerName, @@Version just for fun.
  7. Note that after typing your commands you must type GO and enter before anything you've typed gets sent to the server.
  8. Add yourself to the sysadmin role:
    EXEC sp_addsrvrolemember 'DomainName\LoginName', 'sysadmin'
  9. Or, enable the sa login and set the password with 2 lines of T-Sql:
    Alter login sa With Password= '<enterStrongPasswordHere>'
    Alter login sa Enable
    Go
  10. Exit and close both command windows.
  11. Restart the Sql Server service from the services Gui or with net start MSSQLSERVER or net start MSSQL$instancename

Done.

Equivalent of bash / shell aliases for Windows command line

Relief comes from an unexpected quarter if you pine for your unix command line shell aliases and other such:

doskey ls=dir $*

is what you want. you can put it in an autorun setting in your registry by pasting this into notepad and saving it as something.reg

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="\"C:\\Program Files\\Commands\\CmdAutoRun.cmd\""

If you prefer to do it by hand rather than by double clicking a .reg file, you don't need the extra quotes and escapes (but keep the outermost quotes):

"C:\Program Files\Commands\CmdAutoRun.cmd"

Note you use $* instead of %*. You can otherwise use $1-$9. Further escape codes are on the technet DosKey page