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>

No comments:

Post a Comment