Showing posts with label Mac OS X. Show all posts
Showing posts with label Mac OS X. Show all posts

Saturday, October 30, 2010

Troubleshooting for porting an ASP.NET 4 MVC app from .NET to Mono on Mac OS X

If you received this kind of error while porting a fully functioning existing ASP.NET 4 MVC app to Mono on Mac OS X or other *nix ...

Server Error in '/htmlencoder' Application

Compilation Error
Description: Error compiling a resource required to service this request. Review your source file and modify it to fix this error.
Compiler Error Message: CS0246: The type or namespace name `Dictionary' could not be found. Are you missing a using directive or an assembly reference?
Source Error:
Line 8: 
Line 9:     Input
Line 10:     <p><%: Html.TextArea("Encode","", new Dictionary<string,object> { { "cols", 120 }, { "rows", 12 } }) %></p>
Line 11: 
Line 12:     

Source File: /Library/WebServer/Documents/HtmlEncoder/Views/Encoder/Index.ascx  Lines: 10, 27

Version information: Mono Runtime Version: 2.8 (tarball Thu Oct 7 12:23:27 MDT 2010); ASP.NET Version: 4.0.30319.1


...then add the necessary namespace to root Web.config of your app(On why it works without including the concerned namespace on .NET is beyond me. Might be some namespace in .NET is automatically included on an ASP.NET MVC app, or Mono is more consistent):

<add namespace="System.Collections.Generic" />

Sample Web.config for adding a namespace in it:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->

<configuration>
  <system.web>
    <httpRuntime requestValidationMode="2.0" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>

    <pages>
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Collections.Generic" /> <!-- add this -->
      </namespaces>
    </pages>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Monday, October 25, 2010

ASP.NET on Mac OS X Snow Leopard at one fell swoop using mod_mono

Enable Apache first by ticking the checkbox for Web Sharing(System Preferences > Sharing > Web Sharing)




Type 127.0.0.1 in browser's address bar, then it should show:




Install gettext first: https://gist.github.com/1647940


Install Mono, get it at: http://www.go-mono.com/mono-downloads/download.html

Verify on Terminal if you have installed Mono...

mono -V

...should show this:
Mono JIT compiler version 2.10.9 (tarball Mon May  7 20:25:51 EDT 2012)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
 TLS:           normal
 SIGSEGV:       normal
 Notification:  kqueue
 Architecture:  x86
 Disabled:      none
 Misc:          debugger softdebug 
 LLVM:          yes(2.9svn-mono)
 GC:            Included Boehm (with typed GC)


Paste this in your terminal(the next swoop will still have the effect of sudo(i.e. no need to re-enter password)) :
sudo mv /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/x_index.html.en

And this(so that makes it two swoop ;-)) :
mkdir ~/swoop
cd ~/swoop
sudo ln -s /Library/Frameworks/Mono.framework/Commands/pkg-config /usr/bin/pkg-config
curl -O http://origin-download.mono-project.com/sources/mono/mono-2.10.9.tar.bz2
tar xzf mono-2.10.9.tar.bz2
cd mono-2.10.9
./configure
make 
sudo make install
sudo sh -c "echo '\n\nInclude /etc/apache2/mod_mono.conf' >> /etc/apache2/httpd.conf"
echo "<center><img src='PoweredByMacOSXLarge.gif'><br><% int a = 7 * 6;%><%=a%> <%=new System.Random().Next()%></center>" > /Library/WebServer/Documents/index.aspx 
sudo /usr/sbin/apachectl restart
echo 'Done'


Then refresh the 127.0.0.1 in your browser, you shall be able to see the the answer to life, the universe and everything




This is the how-to video ...