Computers, Programming, Technology, Music, Literature

Archive for September 2013

Writing @helper like inline helper method for ASPX view engine

leave a comment »


This article was originally published for and could be located at

When I started my MVC course with ASP.NET MVC 3, I got introduced to the @helper syntax with the Razor view engine. In case you didn’t know what an @helper syntax is, you may read more about it here. It is common that during training sessions, certain features of a technology may not appeal to you until you really use it, or until you have someone who puts that in perspective for you. So, I was dealing with an MVC application with ASPX view engine and I had a situation where I thought if I had Razor view engine, I could use the @helper syntax, and here’s what I ended up with.


The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates.  They enable better code reuse, and can also facilitate more readable code. – Source


In this post, I will show you can write a Razor style @helper method for ASPX view engine. Some people like to call it the Inline helper method If you look at the end result of the helper method in Razor view engine and ASPX view engine, it not aesthetically similar or lucid, but it solves the purpose – you get re-usability within the view. There are alternative ways of achieving the same thing using a Html helper of a server method within the view, but I ended with this approach.

In the ASPX view engine, if you declare and initialize a variable, if would show up in the intelli-sense. In the same way, if you try declaring an Action delegate, it would show up in the intelli-sense.




And that’s the trick. Declare an Action delegate, initialize with an anonymous delegate. You are done.

        Action<int> DisplayPrice2 = (price) =>
            if (price < 5)
    <%: String.Format("{0:C2}", price)%>

    <h2>Products (ASPX View Engine - with inline helper)</h2>
    <%foreach (var product in Model)
        <span class="producttitle">
            <%: product.Name %>
        <span class="description">
            <%: product.Description%>
        <span class="price">
            <% DisplayPrice2(product.Price); %>
    <%} %>


Note that when you initialize the Action delegate, you could either use the explicit delegate keyword or just a lambda.

Download: Sample projects including the source code for @helper sample in Razor view engine, and the Action delegate way of doing it in the ASPX view engine is downloadable at the SkyDrive location –

Written by gmaran23

September 27, 2013 at 6:19 pm

Posted in .Net, Asp.Net MVC, C#

Tagged with , ,

DI – Unity.Mvc3 and Object reference not set to an instance of an object.

leave a comment »


This article was originally published for and could be located at


When you use Unity.Mvc3 or Unity.Mvc4 for DI, odds are you might not even run in to the below exception/stack trace.

   at Unity.Mvc3.UnityDependencyResolver.get_ChildContainer()
   at Unity.Mvc3.UnityDependencyResolver.GetService(Type serviceType)
   at System.Web.Mvc.BuildManagerViewEngine.DefaultViewPageActivator.Create(ControllerContext controllerContext, Type type)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)


But you’d never know what kind of system you are dealing with. By name, these dlls – Unity.Mvc3, and Unity.Mvc4 – imply that they need to be used with MVC applications; which means, they are many things related to a web page or web request that this dll might possibly expect, for instance, the web context, the authorization context, routing context and so on.

We were dealing with a system where we tried using Unity.Mvc3 for dependency injection, and a very few parts of existing functionalities started failing with the below stack trace.


This exception occurred because we invoke a background worker from the UI (to run a long running Task), and when the background worker finishes, it would call in RenderViewToString, and update the UI. During the second part a System.NullReferenceException was thrown. With the given stack trace it was evident that this was happening due to the Unity.Mvc3 at  Unity.Mvc3.UnityDependencyResolver.get_ChildContainer().

Searching internet yielded no results. We went and looked at the Unity.Mvc3 source code at and found that property named ChildContainer was expecting a HttpContext and our BackgroundWorker didn’t have that for obvious reasons. Hence the Object reference not set to an instance of an object.


Credits to Brijesh, where he had it spot on when he saw the word HttpContext. We ended up removing Unity.Mvc3 completely and used the infamous custom dependency resolver implementing the System.Web.Mvc.IDependencyResolver interface.

Written by gmaran23

September 27, 2013 at 9:58 am

Watch “The security mirage” by Bruce Schneier

leave a comment »



So security is two different things: it’s a feeling, and it’s a reality. And they’re different. You could feel secure even if you’re not. And you can be secure even if you don’t feel it. Really, we have two separate concepts mapped onto the same word. …

…Every species does it. Imagine a rabbit in a field, eating grass, and the rabbit’s going to see a fox. That rabbit will make a security trade-off: "Should I stay, or should I flee?" And if you think about it, the rabbits that are good at making that trade-off will tend to live and reproduce, and the rabbits that are bad at it will get eaten or starve. So you’d think that us, as a successful species on the planet — you, me, everybody — would be really good at making these trade-offs. Yet it seems, again and again, that we’re hopelessly bad at it. And I think that’s a fundamentally interesting question. …

…We tend to exaggerate spectacular and rare risks and downplay common risks — so flying versus driving. The unknown is perceived to be riskier than the familiar. One example would be, people fear kidnapping by strangers when the data supports kidnapping by relatives is much more common. This is for children. Third, personified risks are perceived to be greater than anonymous risks — so Bin Laden is scarier because he has a name. And the fourth is people underestimate risks in situations they do control and overestimate them in situations they don’t control. So once you take up skydiving or smoking, you downplay the risks. If a risk is thrust upon you — terrorism was a good example — you’ll overplay it because you don’t feel like it’s in your control….


Written by gmaran23

September 18, 2013 at 11:02 am

Gimmick free Freebie from Microsoft Translator Preview service

with one comment


I was busy writing the TranslateMaid Addin for Visual Studio and learned so many things in so many ways. One of the many quirks I noticed with the Microsoft Translator Preview service was, when you send an XML element or simply put some text or character enclosed with < and > and some text or character following it, you get a freebie. And what’s that?

For instance, when you send <summary>a as the sourceText parameter to be translated, then the result you yield is <summary>a</summary>. And that’s the freebie.

If this was an expected behaviour, then there is a mistake. If this was a mistake, then there is a mistake in that mistake! I will explain why.

You try with <summary>a, <seealso>a, <returns>a, <Jey>a, <maran>a, <a>a, <sometext>a and for almost everything it returns a free end tag. But when you pass <param>a or <input>a, you don’t get a freebie. They may be many more such examples.

In the below samples, %3C and %3E denote < and > in when URL encoded.



and you don’t get the freebie for


You may also like Gimmick free Fuck from Google translate.

Written by gmaran23

September 11, 2013 at 11:37 am