Arch Linux + lighttpd + Mono == ASP.NET MVC 3

I had a time getting my cheap linux box hosting configured to host my ASP.NET MVC 3 application. These notes are how I got it working using mono 2.6.10 on my Arch Linux 200510

In this example I setup a directory to host my application (I will get to what files and how later) I placed these files in the ‘/srv/http/mysite’ folder.

1. Install mono, lighttpd and fcgi on the box

pacman -Sy
pacman -S mono lighttpd fcgi

2. Setup lighttpd to use FastCGI edit the ‘/etc/lighttpd.conf’ file and modify this line;

server.document-root    = "/srv/http/mysite"

3. Add this line to the end of your ‘/etc/lighttpd.conf’ file;

include "fastcgi.conf"

4. Put this into your ‘/etc/lighttpd/fastcgi.conf’ file;

include "mono.conf"
server.modules += ( "mod_fastcgi" )
fastcgi.server = (
        "" => ((
                "socket" => mono_shared_dir + "fastcgi-mono-server4",
                "bin-path" => mono_fastcgi_server,
                "bin-environment" => (
                        "PATH" => "/bin:/usr/bin:" + mono_dir + "bin",
                        "LD_LIBRARY_PATH" => mono_dir + "lib:",
                        "MONO_SHARED_DIR" => mono_shared_dir,
                        "MONO_FCGI_LOGLEVELS" => "Standard",
                        "MONO_FCGI_LOGFILE" => mono_shared_dir + "fastcgi.log",
                        "MONO_FCGI_ROOT" => mono_fcgi_root,
                        "MONO_FCGI_APPLICATIONS" => mono_fcgi_applications
                ),
                "max-procs" => 1,
                "check-local" => "disable"
        ))
)

5. Put this in your ‘/etc/lighttpd/mono.conf’ file;

index-file.names += ( "index.aspx", "default.aspx", "Default.aspx" )
var.mono_dir = "/usr/"
var.mono_shared_dir = "/tmp/"
var.mono_fastcgi_server = mono_dir + "bin/" + "fastcgi-mono-server4"

### The root of your applications
# For apps installed under the lighttpd document root, use:
var.mono_fcgi_root = server.document-root

### Application map
# A comma separated list of virtual directory and real directory
# for all the applications we want to manage with this server. The
# virtual and real dirs. are separated by  a  colon.
var.mono_fcgi_applications = "/:."

Make sure you didn’t fat monkey up the conf files by doing this;

$ lighttpd -t -f /etc/lighttpd/lighttpd.conf

Should return a “OK”

Now go into your ASP.NET MVC app and setup the references to be “Copy Local” True on these assemblies (if you don’t have the assembly reference by the project add it, you might need to look in “Assemblies -> Extensions” to find some of these) ;

System.Web.Helpers.dll
System.Web.Helpers.xml
System.Web.Mvc.dll
System.Web.Mvc.xml
System.Web.Razor.dll
System.Web.Razor.xml
System.Web.WebPages.Deployment.dll
System.Web.WebPages.Deployment.xml
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll
System.Web.WebPages.Razor.xml
System.Web.WebPages.xml

Make sure you DO NOT have the Microsoft.Web.Infrastructure.dll file, it doens’t work with Mono and causes errors.

Now just publish your project to a folder somewhere, then go into that folder and get the entire contents to the ‘/srv/http/mysite’ folder. Sure you can setup FTP and all that fanciness but I just use WinSCP and copy directly from my publish folder into the ‘/srv/http/mysite’ folder.

Now start lighttpd on the box and you should have the joy of ASP.NET MVC 3.

Enjoy!

References;

http://www.mono-project.com/Release_Notes_Mono_2.10#ASP.NET_MVC3_Support
http://redmine.lighttpd.net/wiki/1/Docs:Configuration
https://wiki.archlinux.org/index.php/Lighttpd
http://www.mono-project.com/FastCGI_Lighttpd
VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Just delete the damn folder…

So I run into a problem where there are folders on my “Storage” drive (like drive “D:” that is way bigger than my boot drive) that for some unknown friggin’ reason I am unable to delete, Windows 7 just gives me some run around and makes me want to take a sledgehammer to the damn thing.

So searching around and I found how to delete it; Change the owner the admin, change the acl’s, then use rmdir from cmd line.

Like so (to whack folder “D:\delete_damnit”);

  • Right-click on folder -> Properties -> Security Tab
  • Click on Advanced button -> Owner tab
  • Edit.. button
  • Select Administrators in ‘Change owner to’, check ‘replace owner on subcontainers and objects’
  • Open cmd.exe as administrator
  • Run ‘cacls D:\delete_damnit /T /e /g Administrators:f’
  • Run ‘rmdir /S D:delete_damnit’

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Adventures in dlna with Blu-Ray players

So the wife and I decided that we would get on NetFlix and be able to stream movies versus going out to BlockBuster. We thought we would go ahead and pick up a new Blu-Ray player that has the streaming built in, and hey while we are at it get one that will also be dlna compliant and connect to my Arch linux powered minidlna media server.

Well getting minidlna setup on Arch via Yaourt is super simple, just a “yauurt -S minidlna” and boom installed. Yay for just works! yay!

So onto getting the Blu-Ray player hooked up to stream pictures, music and video from the dlna server, should be easy enough to get a dlna certified player and, ah, it just happen, right? Pffft.

Off to NFM to pick up a Sony BDP-S470 seems they have it on sale for around $125 and from the manufacturer site it has evertyhing and then some.

I set it up and sure it sees the dlna server, plays the pictures and the music but does not see any movies – no avi, mkv, wmv – nothing. Grrrr. I Make sure the dlna server is working by playing movies via the Xbox 360; works fine indeed.

So return that player and then pick up the Panasonic DMP-BD65, heck its on sale for around $90 and from my cursory search on the net it is dlna certified.

Get it home, hook it up. Humm. Where is the dlna part? Look around the web and find out that the dlna part is only for the European market; not available in the US models. Good grief.

Then off to pick up the LG BD-570 its the most expensive one in my adventure, around $165, but it seems to have Wifi built in and it does have the dlna sticker on the box.

Get it home, hook it up. Endure looks from the wife that I am losing my mind and working on becoming some sort of Blu-Ray exporter. Aha! Eureka right off the bat it sees my dlna server and, wait for it …. Yes! it plays the movies, pictures and music just fine.

Sure the WiFi part stutters a bit playing back some 1080p video (Big Buck Bunny ftw!) but connected on the wired it seems to be excellent in both video and audio.

After some more checking the NetFlix and Vudu works as well. Woot!

Enjoy!

VN:F [1.9.11_1134]
Rating: 7.0/10 (2 votes cast)

Redirect old aspx to new MVC

I upgraded Dvdcrate.com to be ASP.NET MVC and one of the problem is the old pages are indexed by search engines and being served on search results. So I have the old *.aspx page that I need to fix up and send along to the MVC page.

An example of the page in the search database;
www.dvdcrate.com/viewdvd.aspx?upc=5027035006161

And I need it to go to the new MVC controller and action like this;
www.dvdcrate.com/Media/Detail/5027035006161

So I did it this way, I put this in the Global.aspx.cs file;

        protected void Application_BeginRequest (object sender, EventArgs e)
        {
            var requestUrl = Request.Url.ToString().ToLower();
            if (requestUrl.Contains("/viewdvd.aspx"))
            {
                var equalPos = requestUrl.LastIndexOf("=") + 1;
                var upc= requestUrl.Substring(equalPos, requestUrl.Length - equalPos);
                Context.Response.StatusCode = 301;
                Context.Response.Redirect("/Media/Detail/" + upc);
            }
       }

Probably not the most elegant, but it does work.

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

jQuery UI Themeswitcher Cookie Expires

So I disagree with the current setup that is the default configuration with jQuery UI Themeswitcher, it expires the cookie at the end of the session. I mean really if the user is going to change the cookie to some color scheme they like chances are its going to be for longer than the current session and for crying out loud the user is going to expect your site to load the theme they selected last – else it looks broken. Just a poor decision in my opinion – so here is how I fixed it to last 1 year.

Download the themeswitcher.js and save it locally wherever you like.

Now add the “cookieName” below this to the options defition in the file;

    var options = jQuery.extend({
        loadTheme: null,
        initialText: 'Switch Theme',
        width: 150,
        height: 200,
        buttonPreText: 'Theme: ',
        closeOnSelect: true,
        buttonHeight: 14,
        cookieName: 'jquery-ui-theme',
        cookieExpires: 365,
        onOpen: function () { },
        onClose: function () { },
        onSelect: function () { }
    }, settings);

Now modify where the cookie gets made to use this new “cookieExpires” option (line 49);

        $.cookie(options.cookieName, themeName, { expires: options.cookieExpires });

Know you can either use the default set here (365) or set your own in the initializer in your calling script;

    $('#switcher').themeswitcher({ cookieExpires: 365, path: '/', loadTheme: "Sunny"});

Enjoy!

VN:F [1.9.11_1134]
Rating: 8.3/10 (6 votes cast)

Securing ELMAH with ASP.NET MVC

So I like Elmah and its logging of my unhandled exceptions suer, but I don’t need every yahoo with visibility to that. This is the steps I took to only allow Elmah access to users in the “Admin” role and have it still work with ASP.NET MVC.

Put this into the web.config;

  
    
      
        
      
      
        
        
      
    
  

Now put this into your global.asax, up top (before all the other routing) is best;

            routes.IgnoreRoute("admin/elmah.axd");
            routes.IgnoreRoute("admin/{resource}.axd/{*pathInfo}");

You don’t have have any controller action setup, the handler builds this on the fly to handle the incomding axd request. Nice and simple.

Enjoy!

VN:F [1.9.11_1134]
Rating: 10.0/10 (2 votes cast)

Simple merging object to object

I use View Models to hand to my MVC views, nice and slim with little if any methods on them and I use a think ORM model that has all my business logic and is generated by my ORM (BLToolkit).

So the problem is I wanted to take the View Model and update the ORM model with the modified values from the post, kind of a reverse AutoMapper (or BLToolkit.Mapper).

So a ORM class like this;

    public class Contact
    {
         public int Id { get; set; }
         public string Firstname { get; set; }
         public string Lastname { get; set; }
         // Loads of other properties
    }

And a View Model like this (note the type and name should be “EXACTLY” the same;

    public class ContactModel
    {
         public int Id { get; set; }
         public string Firstname { get; set; }
         public string Lastname { get; set; }
    }

Then with this generic Extension method;

        public static void MergeWith(this T1 primary, T2 secondary)
        {
            foreach (var pi in typeof(T2).GetProperties())
            {
                var tpi = typeof(T1).GetProperties().Where(x => x.Name == pi.Name).FirstOrDefault();
                if (tpi == null)
                {
                    continue;
                }
                var priValue = tpi.GetGetMethod().Invoke(primary, null);
                var secValue = pi.GetGetMethod().Invoke(secondary, null);
                if (priValue == null || !priValue.Equals(secValue))
                {
                    tpi.GetSetMethod().Invoke(primary, new object[] { secValue });
                }
            }
        }

So you make the magic in the Controller (for example) like this;

[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult EditContact(ContactModel model)
    Contact c = Repo.Get<Contact,long>(model.Id);
    c.MergeWith<Contact,ContactModel>(model);
    Repo.Update<Contact>(c);
}

Yea its cheesy, but it works for me ;)

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Microsoft Live Writer + WordPress

Wanting to use Microsoft Live Writer to be able to make blog entries I thought I would see what sort of super magic it would take.

To my surprise once you setup your user account to have at least the “Editor” role in WordPress  and then you enable the “XML-RPC” Remote Publishing option – your done. ;)

Super nice. Got to say that so far WordPress is an extremely polished project, there aren’t many open source projects on the same finish. The plug-in architecture and operation is something other projects should envy.

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Moved to WordPress

So I just could not take the spam from friggin’ BlogEngine.net and the fact that project doesn’t have a decent CAPTCHA to help block some of the SPAM is just mindboggling.

I personally don’t like getting 80-100 emails a day from those damn Payday Loan losers, I guess the BlogEngine.NET folks like that sort of stuff.

At any rate I figured WordPress should have lots of plugins for me to mess with and getting Live Writer integrated should be rather simple.

If your looking for some old posts, the search engine on WordPress is pretty good; most likely the page your looking for got moved around. I need to clean up the tags and slugs and all that bloggy stuff; perhaps tomorrow – perhaps.

Enjoy!

VN:F [1.9.11_1134]
Rating: 4.0/10 (1 vote cast)

DotNetOpenAuth MVC Template + VS2010 + Win7 64 == crap;

Well I was playing around with trying to setup a new MVC 2.0 site to use DotNetOpenAuth and I was thrilled to see that the site has a template you can download to make an example site. Excellent I thought!

But of course then I tried it; Using VS210 (10.0.30319.1 RTMRel) and ASP.NET MVC 2 I get this lovely error once I get past the setup.aspx page;

The view at '~/Views/Home/Index.aspx' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.

Humm I figure its something I am doing so I go out and get my Google fu on, I find its a common problem with VS2010 + ASP.NET MVC + Win7 64 Bit. Ah Joy.

So download the VS2008 template, using VS2008, run through the setup.aspx and viola it works like a charm. No funky errors with the template and the redirect they are doing.

Thought I would share as I was kinda sad to see that a VS2010 bug with ASP.NET MVC persisted past QA. So sad.

Enjoy!

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)

Copyright © 1996-2010 Bits, Bytes And Burps. All rights reserved.
iDream theme by Templates Next | Powered by WordPress