Codepolice

Random posts from Ola in English. Mainly about programming and the web.

Meek: The Reversed CMS for ASP.NET MVC

I have a totally custom made website called AlternativeTo. For a long time i’ve been looking for some kind of CMS that you can hook up to just small blocks on the site and just get those small blocks editable with some kind of CMS. I don’t know how common this need is but i would think it is. I have thought about buildning something myself but i was pretty sure that there must be someone out there that had the same problem before me, and why invent the wheel and so on.

Anyway, some days ago i finally found the solution and it’s called Meek. Meek was exactly what i described above. Extremely simple and it’s installed by just referencing a DLL and about 3-4 lines in web.Config. Well you can read all about it at their homepage.

I can also mention i had a small issue mostly because i have kind of a strange setup. I had to add the custom route because i didn’t have the default route in my Web.Config.

routes.MapRoute(
 "Meek", // Route name
 "Meek/{action}/{id}", // URL with parameters
 new { controller = "Meek", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

I also had to add the init line manually to my Application_Start method. I recorded a short video how it works on YouTube.

Link: Meek

What I Love So Far With Visual Studio 11

I have used Visual Studio 11 since it was released in beta and i must say i really like it. I wanted to write down my favorite features here both to remind myself about them and to maybe give any of you random readers some tips.

HTML Editor / ASP.Net

  • You can now generate a event handler directly from the design source view. Before you had to go to the design view or use the “tab tab” feature in the code view to auto generate an event handler. Now you can do it from the asp server tag itself.
  • Just as you can extract a method via the refactor context menu in the code editor you can now Extract a user control from the HTML view. So if you have a chunk of HTML that you feel would fit better in a user control you can just select it all, right click on it and choose to extract as a user control. VS will then create the new user control, reference it in the document and insert a tag for it. You have to move any code that reference any of the elements that you extracted manually though.
  • If you change a a HTML tag in VS11 it will now also change the closing tag. It’s strange that this hasn’t been there before but I’m glad they finally added it.

CSS Editor

  • I’m a CTRL K+D junkie and use it all the time to format my code. What bugged the hell out of me before was that in a CSS file the editor always scrolled up to the top of the document when i did this. It doesn’t do that anymore. In relation to this VS11 also intend CSS based on class. So .header h1 will get intended below .header.
  • As much as I use the shortcut to format my document i use CTRL K+C  and CTRL K+U to comment and uncomment my code. This did not work in CSS files before but works now.
  • I use border-radius and some other CSS3 features a lot and since some browsers requires vendor specific prefix for these it’s always a mess to have to write -moz-bla-bla and -webkit-bla-bla and so on. If you type border-radius in the Visual Studio 11 editor and then press tab twice it will automatically create these.

Page Inspector

The Page Inspector they added in Visual Studio 11 is just brilliant! It’s like Firebug but instead of seeing your generated HTML when you inspect elements you see the actual server side code that generates a specific element on your site. And it also works with CSS! No more hacking in Firebug to make something looks good and then remember what you changed and save it in the actual file and so on. As i said, it’s just brilliant! Right click on the project and choose “View in Page Inspector” to use it!

 

Get the Dark Theme for the editor but not for the rest of the UI in Visual Studio 11

I just downloaded the shiny new Visual Studio 11 BETA and haven’t had so much time to use it yet. But one thing i was curious about was the new built-in dark theme they provided. I really liked the Dark Theme for the code editing windows but not so much for the rest of the UI. I solved this by just switch to the dark theme, export the settings, switch back to light theme then import the settings and unchecked everything but the colors & fonts. Worked like a charm! I recorded a video of it below.

Import blog from Tumblr to WordPress can totally destroy your blog if you do it wrong

We decided to move our AlternativeTo blog from Tumblr to WordPress some days ago and i just want to share some of my experience about that process. To begin with I didn’t get the importer that used the Tumblr API to import the blog at all. I could log in and everything but absolutely nothing happened. I waited for at least one hour before i gave up.

But it was when i tried to use some other importers that the problems got really serious. I did some googling and i found a site called Tumblr2Wordpress, beware of this exporter if you use WordPress 3.0. It says nothing on this site at all but as soon as you import to WordPress via the XML file produced from this site your site is more or less screwed. I got an error saying “Are you sure you want to do this? Please try again.” or something like that everytime i edited a post, saved a draft or tried to publish a post. I spend hours debugging this before i figured out it was this importer that caused this.

Finally i looked on the page about importing on WordPress.org and they actually warns about the Tumblr2Wordpress site and they provided a link to a site called Tumblr2wp that actually worked. To bad that site didn’t have better rank in Google, would have saved me hours of work and frustration. Hopefully someone will find this post at least if you are getting the same problems i had.

Make the Git GUI appear after you publish a Web Project in Visual Studio 2010

A couple of moths ago i started to use GIT as my primary Source Control. To be honest I hadn’t use any “real” source control at all before for this project. I only used a online backup service that had some basic version control on a file to file basis.

But i love GIT. It’s really simple and everything works more or less exactly as you expect it to do. But there isn’t much tools out there that integrates well with Visual Studio. There is Git Extensions for Windows and there is a project called Git Source Control Provider. The problem with Git Extensions is that it doesn’t do that much. It only ads a menu in Visual Studio where you can execute some command. Git Source Control Provider on the other hand looked like the perfect tool for me. But for some reason it didn’t sync correctly with my Git Repository and i got the feeling the project was a bit to immature to be trusted.

I always forget to commit

So, my biggest problem is that I always forget to fire up the command line / GUI and actually commit the changes i do the code. When you use SVN, TFS you get those nice little icons on all the files in Visual Studio so you are constantly reminded of your changes. This was what Git Source Control Provider promised but as i said, didn’t work for me.

I would be happy just to be reminded every time i publish my website to the ftp that “Hey! Don’t forget to commit your changes!”. And just to be clear. This is a solution for a one man shop that don’t really need “continuous integration” and loads of complicated stuff. I like my super simple solution where i just publish my project to my ftp and boom done!

My stupid hack

I found this post on Stack Overflow that explains how to run a MS Build target after you do a publish from Visual Studio. You just have to add this target at the end of your .csproj file. Your publish method must be a “WebDeploy”. I tried with “File System” but that didn’t work. The “WebDeploy” method was much faster anyway.

<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish" >
    <Exec Command="&quot;C:\Program Files (x86)\Git\bin\wish.exe&quot; &quot;C:\Program Files (x86)\Git\libexec\git-core\git-gui&quot;" />
</Target>

That’s more or less all there is to it. This will fire up the GIT GUI and your Publish task won’t finish until you close that program. Of course you could probably just run the console instead if you prefer that.

Copy a folder with Robocopy

I’m playing around a bit with PhoneGap at the moment. I wanted to make a script that copies all my javascript, html, images and css from my Visual Studio project folder to each of the diffrent PhoneGap projects for iOS, Android and so on.

Visual Studio has a featured to execute commands after the project is built and it felt like the simplest solution. I tried to add a command like robocopy c:\myproj\css c:\myandroidproj it worked but the thing was that i wanted to copy the folder AND the files in it and i couldn’t figure out how to to that. Maybe I’m just stupid, i don’t know.

Anyway i figured out how to do it in a good way finally.

robocopy $(ProjectDir) C:\Java\MyProj\assets\www\ *.html *.js *.css *.png *.gif *.jpg /S

This will copy all the specified filetype, ignore files like .csproj and stuff like that. The S stands for “Copy directory structure but ignore empty folders”.

Batch Optimize Png, Jpg and Gif images in Windows (and Visual Studio)

A couple of days ago i found an awesome extension to Visual Studio that let’s you just right click an a folder in VS and it will optimize all the images in it with the help of SmushIt and PunyPng. I have tried to find a tool like this for a long time but haven’t really found anything that suits my need. I also wanted this functionality in Windows outside of Visual Studio so i downloaded the code and made a extremely simple command prompt application for it. It would of course be even better if you could just right click a folder in Windows to run it but I’m a lazy person and this works for me.

Just run BatchOptimzeImage.exe <foldername> and it will optimize all the images in that folder. I wont give so much more instructions than this. And again .. ALL credits goes to Mads Kristensen for this on. I just wanted to put this out there so you don’t have to spend the hour i spent to make a console app out of it.

Download BatchImageOptimizer

Automatically switch spell check language/dictionary as you type in Firefox

I have been looking for a plugin like this for ages. I constantly switch between Swedish and English when i work and it’s a pain to always remember to switch the spell check to the correct language. But today i found Dictionary Switcher that automatically set the spelling language to the locale of the page but also detect what language you are typing in and switch to the correct language. I’ve made a YouTube Video to show how it works.

Custom Action Result do not run ExecuteResult and just returning ToString

All of a sudden all of my RssFeeds stopped working on AlternativeTo. All i got was the name of the type i returned from my MVC methods that should return a "RssResult". After some Googling i finally found the answer on Stack Overflow as usally.

The problem was that i used version ;MVC 2 in some of my projects (the one where the code for the Custom Action Result was located) and version MVC 3 in some other (the web project).

The thread on Stack Overflow about the issue.

RouteDebugger and 404 MVC Json requests

I got this really strange bug where i got a 404 error on all request i made via Ajax to methods that returned Json via MVC. Turned out that this was caused by RouteDebug that i had laying around in my bin folder. I have got this bug twice now so i just wanted to make a note of this here so i remember it next time.