Visual Studio Template ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588

If you use the VS template to create a new 'internet' web application, then the template includes code for Asp.Net simple membership. But the template is written for SqlExpress. If you instead have a full SQL Server install on your machine it won't work.

The first exception you might see when debugging in visual studio is,

Exception has been thrown by the target of an invocation.

or if you aren't debugging you might see

The system cannot find the file specified

neither of which help at all.

so first, change the connection string in web.config to use your local SQL Server:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication1-20140225162244;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication1-20140225162244.mdf" providerName="System.Data.SqlClient" />

changing the connectionString's Data Source property to Data Source=.;

Now you might get the same or a different exception message:

The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588

but the linked page doesn't tell you how to fix it.

The inner exception is more helpful:

Directory lookup for the file "c:\...etc...\MvcApplication1\App_Data\aspnet-MvcApplication1-20140225162244.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

Which tells you that the login account under which SQL Server is running doesn't have write permissions to the directory in which you write you code. It does have write permissions in the SQL Server data directory, for instance C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA so you'd think that by looking at that directory to see what user it runs under you'd be able to give it permissions.

Almost. The Explorer GUI Security tab showed me a usergroup called 'MSSQLSERVER' but if you try to give permissions to that group you'll find it doesn't exist. More helpful is the command line:

cacls "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL"

which, if you look carefully at the output,

C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL 
    CREATOR OWNER:(OI)(CI)(IO)F
    NT AUTHORITY\SYSTEM:(OI)(CI)F
    BUILTIN\Administrators:(OI)(CI)F
    BUILTIN\Users:(OI)(CI)R
    NT SERVICE\MSSQLSERVER:(OI)(CI)R

shows you that you the actual name is NT SERVICE\MSSQLSERVER

So I gave modify permissions on my App_Data directory to NT SERVICE\MSSQLSERVER (I carefully copy-pasted the full name) and it worked.

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.

MVC Html.ActionLink() fails weirdly with routeValues parameter in Asp.Net

It seems simple. In an MVC view page you have an @Html.ActionLink with parameters for action, controller & route values:

@Html.ActionLink("Some text","Action","Controller", new {id=1})

but instead of rendering the text with a link to the action, it renders a link with some weird URL that isn't what you want at all.

If you use intellisense to look at the overloads for ActionLink, you'll see that the overload you've got is interpreting new {id=1} as html attributes not as route values. You need the overload with one more parameter, which can be null:

@Html.ActionLink("Some text","Action","Controller", new {id=1}, null)

And then it works.