Scott Hanselman

Exploring ASP.NET Web Pages - A fully-featured MiniBlog using just Razor

April 16, 2014 Comment on this post [35] Posted in ASP.NET | Open Source
Sponsored By

ASP.NET "Razor" Web Pages are ASP.NET sites without models, views, controllers, or project files. Some folks say "oh, that's just Classic ASP, or PHP right? Not at all. It's the full power and speed of the .NET CLR, the full syntax of C#, LINQ, along with things like C# dynamics. It's super powerful, and my friend Mads and I are surprised more people don't use them for small things.

In fact, Rob Conery and I did the http://thisdeveloperslife.com web site using just Razor and Rob's "massive" micro-ORM. Later I made http://hanselminutes.com with Web Pages as well.

This blog runs DasBlog, an older ASP.NET 2.0 blogging engine I worked on with Clemens Vasters and a lot of co-contributors, but I'm actively checking on Mads' MiniBlog, a minimal but VERY competent blog engine using Razor Web Pages. Why wouldn't I use something like Ghost? I've thought about it, but MiniBlog is SO minimal and that makes it very attractive.

Here's some things I like about MiniBlog, as both a blog and a learning tool.

Minimal

It's not called Mini for fun. There's a truly minimal packages.config of dependencies:

<packages>
<package id="AjaxMin" version="5.2.5021.15814" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="xmlrpcnet" version="3.0.0.266" targetFramework="net45" />
<package id="xmlrpcnet-server" version="3.0.0.266" targetFramework="net45" />
</packages>

Clean use of Handlers for Web Services

Blogs do more than just serve pages, there is also a need for RSS feeds, MetaWeblog Web Services for things like Windows Live Writer, and dynamic minification for JS and CSS.

<handlers>
<add name="CommentHandler" verb="*" type="CommentHandler" path="/comment.ashx"/>
<add name="PostHandler" verb="POST" type="PostHandler" path="/post.ashx"/>
<add name="MetaWebLogHandler" verb="POST,GET" type="MetaWeblogHandler" path="/metaweblog"/>
<add name="FeedHandler" verb="GET" type="FeedHandler" path="/feed/*"/>
<add name="CssHandler" verb="GET" type="MinifyHandler" path="*.css"/>
<add name="JsHandler" verb="GET" type="MinifyHandler" path="*.js"/>
</handlers>

MiniBlog uses .ashx (HttpHanders) and wires them up in web.config. RSS feeds are easily handled with System.ServiceModel.Syndication, even JavaScript and CSS minification. Though MiniBlog is very new, it uses the old but extremely reliable CookComputing.XmlRpc for the MetaWeblog service communication with Windows Live Writer. I

No Database Need

I like apps that can avoid using databases. Sometimes the file system is a fine database. I thought this when we worked on DasBlog, Mads thought it when he made BlogEngine.NET (his original blog engine) and that "no database needed" design tenet continues with MiniBlog. It stores its files in XML, but MiniBlog could just as easily use JSON.

Clean Content-Editable Design Service

I always (exclusively) use Windows Live Writer for my blog posts. WLW is also the preferred way to write posts with MiniBlog. However, if you insist, MiniBlog also has a really nice content-editable scheme with a great toolbar, all in the browser:

Nice Editing Experience

When you are viewing a post while logged in as Admin, you click Edit and turn the page into editable content.

editPost = function () {
txtTitle.attr('contentEditable', true);
txtContent.wysiwyg({ hotKeys: {}, activeToolbarClass: "active" });
txtContent.css({ minHeight: "400px" });
txtContent.focus();

btnNew.attr("disabled", true);
btnEdit.attr("disabled", true);
btnSave.removeAttr("disabled");
btnCancel.removeAttr("disabled");
chkPublish.removeAttr("disabled");

showCategoriesForEditing();

toggleSourceView();

$("#tools").fadeIn().css("display", "inline-block");
}

The resulting HTML you write (in a WYSIWYG mode) is converted into XHTML and posted back to MiniBlog:

parsedDOM = ConvertMarkupToValidXhtml(txtContent.html());

$.post("/post.ashx?mode=save", {
id: postId,
isPublished: chkPublish[0].checked,
title: txtTitle.text().trim(),
content: parsedDOM,
categories: getPostCategories(),
})

The JavaScript is surprisingly simple, and gets one thinking about adding basic editing and CMS functions to websites. A design mode would be a daunting task 5 years ago, and with today's JavaScript it's almost trivial.

It even automatically optimizes images you drag and drop into the design surface and upload.

public static string SaveFileToDisk(byte[] bytes, string extension)
{
string relative = "~/posts/files/" + Guid.NewGuid() + "." + extension.Trim('.');
string file = HostingEnvironment.MapPath(relative);

File.WriteAllBytes(file, bytes);

var cruncher = new ImageCruncher.Cruncher();
cruncher.CrunchImages(file);

return VirtualPathUtility.ToAbsolute(relative);
}

The code is fun to read, and you can go check it out at https://github.com/madskristensen/MiniBlog. It supports HTML5 microdata, sitemaps, both RSS and Atom, simple theming, and gets a 100/100 of Google Page Speed.


Sponsor: Big thanks to Red Gate for sponsoring the feed this week. 24% of database devs don’t use source control. Do you? Database source control is now standard. SQL Source Control is an easy way to start - it links your database to any source control system. Try it free!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
April 16, 2014 10:26
saw it, loved it, forked it
April 16, 2014 11:12
This is pretty cool. I really like Web Pages and am surprised more people don't use it to create simple web sites. It beats WebForms and MVC when it comes to simplicity and learning curve.
ed
April 16, 2014 11:22
scott , fantastic. any idea if Mads is thinking of providing some guide lines on how to migrate from Wordpress to miniblog ?
April 16, 2014 12:16
@Lohith: There is a side project "MiniBlogFormatter" which converts the Wordpress/DasBlog Exports to the MiniBlogFormatter "Format" https://github.com/madskristensen/MiniBlogFormatter
April 16, 2014 12:17
That is more than just nice :)

Actually by moving the application logic behind API the only really valuable thing in .NET-based UI is the ability to use the MasterPage (WebForms) or ViewMasterPage (MVC) - to keep all common stuff like general page layout, CSS/JS/etc. links in one place.

I've been meaning to try to do that but just didn't have the time. Anybody tried?
April 16, 2014 12:27
I migrated from Blogengine.net to miniblog a while ago and the performance is impressive.
April 16, 2014 13:04
Web Pages and Razor are fast and super light weight. I used it to build Career Voyage and it has performed flawlessly for the past year with over 4 million page views by 50K+ users. Its the hidden gem of the ASP.NET stack!!
April 16, 2014 14:17
Razor Pages are awesome. I used them along with Webmatrix to write my own 'blog engine' called BlogMatrix.

It is highly tailored to my own needs and wasn't really written with others in mind :)

Some of its features:
- Free hosting with GitHub Pages
- Uses git to deploy
- Razor Pages goodieness (layouts, partials etc)
- Responsive design via Twitter bootstrap
- No database
- JSON used for metadata and populates the RSS feed
- 'Compiles' down to pure HTML through the use of wget
- WebMatrix for my GUI editor

Anyway checkout my blog post series (which is using BlogMatrix): BlogMatrix. A Very Simple Blog Engine

Kestrel
April 16, 2014 14:26
I've just tried connecting Writer to MiniBlog, but its coming back saying it cant find the template and asking me to choose which blogging platform it is. Anyone else have this, or know which URL its after?

James.
April 16, 2014 14:53
After looking some more, to get Writer to 'see' the blog.

The Remote Posting Url was "http://blogaddress/metaweblog"
April 16, 2014 15:13
That is sweet! Hard to beat Mads, but a blog engine to watch is sblog. The author doesn't seem to have much love yet. But it beats the others I've played around with.
April 16, 2014 15:26
Too bad I didn't hear about it before starting my blog this winter, the Razor engine is great to work with. Wordpress has a lot of features, but it's overkill for many projects.
April 16, 2014 16:53
Scott,

Do you know if miniblog supports arbitrary standalone pages? I'm currently using Orchard, which is a fine CMS, but arguably overkill for a blog, and I find myself longing for something simpler.

Anyone else on Orchard want to work together on a formatter to export Orchard content to miniblog?
April 16, 2014 17:18
Razor, WebPages, ASP, ASP MVC, LightSwitch, WebMatrix, etc. etc.

By the time I learn the names and what they do and how they connect, Microsoft renames them all and I have to start all over again!

Sigh...
April 16, 2014 17:48
Highly interested in building with this tool to support some podcasting I plan to do. Will be grabbing the bits tonight. Does it support LESS/SASS?
April 16, 2014 18:12
@John: It doesn't look like it natively, but given how simple the .js/.css minifier handler looks, I'm sure you could wire up your own LESS/SASS handler to get that support. https://github.com/madskristensen/MiniBlog/blob/master/Website/app_code/handlers/MinifyHandler.cs
April 16, 2014 18:45
I migrated from Blogger to MiniBlog a few months ago. I was about to write something myself (something lightweight and built with MVC). But then I found MiniBlog and it's been a perfect fit. I like that it works with Windows Live Writer.
April 16, 2014 19:58
@John,

You can easily use LESS or Sass with MiniBlog if you use Web Essentials or any other VS extension that can compile it. On my blog, which also runs MiniBlog, I've made such modifications. For instance, I don't use the minification HttpHandler, but instead uses Web Essentials for both bundling and minification.
April 16, 2014 20:15
Great article. I like it.
April 16, 2014 20:17
@Andrew,

You should ask Brady Gaster (http://bradygaster.com/) who made the switch last year. I think he has some code to do that. See the irony when you start asking for more features like adding pages, but without the overkill ;)
April 16, 2014 20:24
@Sebastien,

I fully appreciate the irony, and I also appreciate all the hard work you've put into Orchard. It's a tough balance between feature-richness and complexity vs. simplicity.

:-)
April 16, 2014 21:23
Checkout MvcPages, MVC without routes and controllers, or WebPages with Model Binding, Model Validation, Editor/Display Templates, strongly-typed HTML helpers, TempData, etc.
April 17, 2014 1:26
Can I just drop in the BlogEngine.Net XML file(Posts) into MiniBlog?
April 17, 2014 2:49
I also love to use plain .cshtml files for 'general' html pages that need a few small server side commands embedded and using Razor instead of the WebForms syntax is definitely way nicer.

However, one reason I don't use .cshtml as much as I should is that it's not automatically configured. For example, when distributing demos or NuGet packages, you can't be assured that .cshtml just runs. You have to specify the <appSettings> keys to specify that WebPages are enabled and worse - you have to specify the version. Plus you may have to add additional NuGet packages to get the right version of WebPages installed. Compared to ASPX which is just there and works that's can be a pain for casual use. I'd love to see .cshtml work the same as ASPX does without required configuraiton.
April 17, 2014 12:43
I get

The page cannot be displayed because an internal server error has occurred.

While trying to create a new post. anything wrong ?
April 17, 2014 13:45
I also get an error when creating a new post on the following line in PostHandler ProcessRequest:


if (!context.User.Identity.IsAuthenticated || Blog.MatchesUniqueId(context))
throw new HttpException(403, "No access"); //this is thrown
Rik
April 17, 2014 22:38
ASP.NET Web Pages sound very cool. So, how do I add ASP.NET Web Pages to an existing MVC application that uses .net 4.5?
April 18, 2014 2:46
I'm having the same trouble James was. I am brand new to blogging, just downloaded the code and published directly to azure. When attempting to connect WLW to the site, I'm told by WLW that it can't automatically detect my blog settings and I need to select one from the list (upon which miniBlog is *not* on said list) and to provide the "Remote Posting URL" whatever that is.
Can someone lend a little guidance here for a newby?

Thanks!
April 18, 2014 3:27
Figured it out! For WLW, select "Metaweblog API" from the drop down and then add "metaweblog" to the end of your url for the Remote Posting URL e.g. "http://mynewblog.azurewebsites.net/metaweblog" and then WLW should pick it up.
April 18, 2014 12:33
great Ideas i l like .........<3
April 19, 2014 9:25
I don't understand the continuing preference for XML-like config files. You said the config is truly minimal, but compare this:


<packages>
<package id="AjaxMin" version="5.2.5021.15814" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="xmlrpcnet" version="3.0.0.266" targetFramework="net45" />
<package id="xmlrpcnet-server" version="3.0.0.266" targetFramework="net45" />
</packages>


with this:
{
packages: {
'AjaxMin': '5.2.5021.15814',
'Microsoft.AspNet.Razor': '3.0.0'
'Microsoft.AspNet.WebPages': '3.0.0',
'Microsoft.Web.Infrastructure': '1.0.0.0',
'xmlrpcnet': '3.0.0.266',
'xmlrpcnet-server': '3.0.0.266'
},
targetFramework: 'net45'
}


Much better!
April 20, 2014 23:29
I would like to install MiniBlog as a component of my existing ASP.NET MVC Web Application. I have asked about this on StackOverflow here.

How would you integrate MiniBlog as a module of an existing Web App?

My sense is that ASP.NET MVC Areas might be a good approach. I'm not sure, though, how to place an existing Web Site into an Area.
April 21, 2014 2:15
It's pretty awesome, I would only add markdown support since it has nice built in code highlighting.
May 02, 2014 9:14
MiniBlog with Ghost Editor: link.
July 14, 2014 6:44
It's really very nice idea.I liked these pretty ideas about ASP.NET.If you want to get more knowledge about ASP.NET you can visit myasp.net.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.