This forum is in archive mode. You will not be able to post new content.

Author Topic: Running MVC3 + Razor view engine on mono on linux  (Read 6875 times)

0 Members and 1 Guest are viewing this topic.

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
Running MVC3 + Razor view engine on mono on linux
« on: October 14, 2012, 03:59:39 PM »
I started learning ASP.NET but since I'm a linux user, I'm using mono rather than Visual Studio. The IDE I'm using is mono-develop but unfortunately, it doesn't come with the razor view engine built in. So heres a quick tutorial on how to get razor view engine working on mono.

Note: The latest version of mono at the time of writing this is Mono 2.10.5 and the version I am running it on Ubuntu.

So in monodevelop, start up a new solution then select C# > ASP.NET MVC Project. Then click Run > Run to compile and run the project so it will create the bin directory which holds the assemblies. First thing you need to do is add some new assemblies which you get from a Visual Studio project. The assemblies are:
System.Web.Helpers.dll
System.Web.Mvc.dll
System.Web.Razor.dll
System.Web.WebPages.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.Razor.dll

The tutorial I was reading didn't provide these assemblies and I didn't have Visual Studio so I had to install it on a Windows VM (which took a few hours) which was a pain in the ass. To save you the hassle, here are the necessary assemblies I got from a Visual Studio project:
http://www.4shared.com/archive/MUUdxjAY/assembliestar.html
So go to the bin directory and delete Systems.Web.Mvc.dll (thats the one created by mono, you need to replace it with the one created by Visual Studio) then transfer the assemblies (from the tar.gz archive) to the bin folder.

Now in the solution menu in monodevelop, expand the References tab and delete Systems.Web.Mvc from the list. Now right click on the references tab, then click Edit References. Click the .NET Assembly tab, go into the bin folder then select the new assemblies to add.



Now you just need to update the Web.config file. Edit the file and replace its contents with the following:
Code: [Select]
<?xml version="1.0"?>

<configuration>
    <configSections>
       
       
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
       
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
    </configSections>
   
   
    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
          <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
          </namespaces>
        </pages>
    </system.web.webPages.razor>
   
    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>
   
    <system.web>

        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->

        <compilation defaultLanguage="C#" debug="false">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
                <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
            </assemblies>
        </compilation>
       
        <customErrors mode="RemoteOnly">
        </customErrors>

        <pages
            validateRequest="false"
            pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <controls>
                <add tagPrefix="mvc" namespace="System.Web.Mvc" assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            </controls>
            <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.Linq" />
                <add namespace="System.Collections.Generic" />
            </namespaces>
        </pages>
       
        <authorization>
            <allow users="*" />
        </authorization>
       
        <httpHandlers>
            <remove verb="*" path="*.asmx" />
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
            <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpHandlers>
       
        <httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpModules>
       
        <trace enabled="false" localOnly="true" pageOutput="false" requestLimit="10" traceMode="SortByTime" />
        <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
    </system.web>
   
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5" />
                <providerOption name="WarnAsError" value="false" />
            </compiler>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5" />
                <providerOption name="OptionInfer" value="true" />
                <providerOption name="WarnAsError" value="false" />
            </compiler>
        </compilers>
    </system.codedom>
   
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
   
        <handlers>
          <remove name="BlockViewHandler"/>
          <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
    </system.webServer>
   
    <system.web.extensions />
</configuration>

And finally, to allow mono to compile the project without crashing, In the solution menu, right click on your project, then click Options. Select the Build > General tab, then in the dropdown menu, select .NET 4.0:


Now to check if razor works, edit Views/Home/Index.aspx and replace the code with the following:

Code: [Select]
@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC
Website">http://asp.net/mvc</a>
</p>
then rename Index.aspx to Index.cshtml. Compile and run the project. If it works, the page should look like this:


The tutorial I followed is this:
http://blog.yojimbocorp.com/2012/08/27/asp-net-mvc3-with-razor-view-engine-using-monodevelop-and-ubuntu-12-04/
Most of what I said was from that tutorial but the Web.config file is new. When I used the Web.config file provided by that tutorial, it caused a problem that prevented the browser from being able to access static files. I made a new config file by adding the necessary code to the default Web.config file generated by mono. I also provided the necessary assembly files so people who don't have Windows and Visual Studio don't have to waste time googling for them.

BTW, if you've used Visual Studio before, you'll notice that when you start an MVC project with VS, it generates layout templates, style sheets, scripts and lots of other things by default, whereas mono more or less just generates the bare necessities. If you want the extra stuff that comes with a Visual Studio MVC project, heres the files from a VS MVC3 project:
https://rapidshare.com/files/877254223/MvcApplication.tar.gz
« Last Edit: October 15, 2012, 07:09:04 PM by m0l0ko »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.