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

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

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

        <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 -->

    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>

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

No comments:

Post a Comment