IronShay

Ironing code, geek t-shirts and even presentations!

NAVIGATION - SEARCH

Razor Tip #2: Output Text in Code Context

Razor, the new ASP.NET MVC view engine is incredible. I like it. A LOT. Great work Microsoft!

In this series of posts I’m sharing some handy tips and tricks that can enhance your experience with this new view engine. Enjoy!

The Problem

Razor is the place where HTML and C# live together in harmony. This is, in my opinion, one of the things that make razor the great view engine that it is. However, there’s a fly in the ointment. Assume you want to output “Good Morning!” if the hour is between 6AM and 9AM:

Razor Tip #2

But oh no! Razor thinks “Good Morning!” is code!

The Solution

Razor has a special solution for that problem. Three different solutions for the matter of fact:

  • Writing the text inside an HTML element – razor knows to differentiate between C# and HTML so if you wrap the text with an HTML element, everything will work as expected:
    Razor Tip #2
  • Adding the “@:” symbol at the beginning of the line – using this symbol will tell razor that this line is an output line and should not be treated as code:
    Razor Tip #2
  • Using the special <text></text> element – when you want to output several lines within code context, it becomes irritating to use the “@:” symbol… For that you’ve got the special <text></text> element which tell razor that its content is to be outputted as HTML. Notice that the <text> element will not be outputted to the final HTML output.
    Razor Tip #2

All the best,
Shay.



Razor Tip #1: Explicitly Stating Statement Boundaries

Razor, the new ASP.NET MVC view engine is incredible. I like it. A LOT. Great work Microsoft!

In this series of posts I’m going to share some handy tips and tricks that can enhance your experience with this new view engine. Enjoy!

The Problem

You have to output a variable value and add some markup content immediately after it. For example, assume you have a variable holding a value representing a font size and you want to output it inside a style attribute, as follows:

image

But oh no! Razor can’t tell the difference between our fontSize variable and the “px” text that follows it so it will go look for a variable named “fontSizepx”!

The Solution

To solve the issue, we need to explicitly tell Razor where the variable name ends, or in more general terms – where the statement starts and ends. We do that by putting the variable name between parentheses:

image

You can also use the parentheses trick to output small code statements like these:

image 

All the best,
Shay.



LINQ Tip: Chain Ordering

Assuming you have the next code:

public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

public class Whatever
{
  public void Do()
  {
    var people = new List<Person>
                  {
                     new Person {Name = "Shay Friedman", Age = 27},
                     new Person {Name = "Shawn Doe", Age = 51},
                     new Person {Name = "Elvis Presley", Age = 76}
                  };            
  }
}

And now you want to order it first by name and then by age using LINQ. If you were to do that:

var orderedPeople = people.OrderBy(p => p.Name).OrderBy(p => p.Age);

You would get incorrect results:

LINQ Tip: Results when using OrderBy after an OrderBy call.

That’s because the second OrderBy call just overrides the first call results. To fix that, use the ThenBy LINQ method:

var orderedPeople = people.OrderBy(p => p.Name).ThenBy(p => p.Age);

And now everything works as expected:

LINQ Tip: Using ThenBy after an OrderBy call.

All the best,
Shay.



Wrapping Up my “Swedish Tour”

File:Flag of Sweden.svgI’ve just got back home after a week in Sweden. During the week I had the opportunity to travel the country a bit, to learn a few Swedish words and to speak in a few occasions about ASP.NET MVC, IronRuby and some other technologies. But above all of that, I got to meet and talk with some incredible people! it was such an amazing experience!!! thanks to all of you (you guys know who you are), you made this week one of the best I’ve ever had.

Well, yes. I know I titled this post a “wrap up” and you want the details. So…

 

Part 1 – Stockholm

The first part of my “tour” was Stockholm. I was invited to Stockholm by Tibi to participate in the unconference he had been planning. It had been snowing in Sweden before I got there so I was welcomed by a lot of snow. And I LOOOOOOOOVE snow! I was sad, though, that it wasn’t snowing while I was there…

Tibi hosted me in his place during my stay in Stockholm so huge thanks to him, to his wife Nicolleta and to his sweet daughters for having me!

On the day after I came we had the unconference in a very cool office in Stockholm. I was impressed that even university students showed up for the unconference! good for you! During the unconference we had very interesting and inspiring discussions about various different and unrelated subjects (in the .NET world). I got to talk about IronRuby and it was very interesting for me to hear the questions and the feedback.

Some photos from the event:

.NET Unconference in Stockholm.NET Unconference in Stockholm

Part 2 – SDC2011, Gothenburg

The day after the unconference, me and Tibi took the train to Gothenburg to participate in the Scandinavian Developer Conference, AKA SDC or ScanDev. We got to an amazing hotel called Gothia Towers in Gothenburg where all the speakers were hosted. The conference itself took place in a convention center that is attached to the hotel. This is how the hotel looks like from outside and the inside:

My room in Gothia Towers HotelGothia Towers Hotel from Outside

On the first evening we had a speakers dinner organized by the conference organizers. They took us to a very nice restaurant and I got to meet and talk with other speakers about technologies, languages (real ones! not just programming languages!) and other stuff. These meetings are the best thing in conferences… the opportunity to chat with people from all over the world (face-to-face) is not something that you run into every day.

Anyway, the next day I had my session, The Big Comparison of ASP.NET MVC View Engines, and it went pretty well. I had something like 100 people attending and had lots of fun. Razor won the poll again, by the way. I wrote another post about the session, in case you want to see/download the slides or code samples.

On the evening we had some food at a nice little restaurant, had interesting discussions and lots of alcohol. Everything is a bit blurry for me from this night… I love conferences! :)

On the second day of the conference I didn’t have any sessions so I got to relax, go to some sessions myself and in general, have fun! In the evening we went to a traditional Swedish restaurant and had some traditional Swedish food. It was a blast!

Part 3 – Swenug (Swedish .NET User Group), Gothenburg

The last part of my trip was the Swedish .NET user group meeting in Gothenburg. I had two ~1 hour sessions, one was about tips and tricks in ASP.NET MVC 3 (or the session’s official name – “ASP.NET MVC Rulezzzzzz”) and the second one was about IronRuby and its possible usages for .NET devs (or in its official name, “IronRuby FTW!!!!!!!”). I must say, these were one of the best presentations I’ve ever had. It felt good, I had an awesome awesome AWESOME time, got very good responses afterwards and even all the demos worked!

Thank you Anders for having me and pulling this off.

Summary

It was an amazing week. I had a chance to speak in front of the great Swedish crowd. I met so many interesting people. I ate a reindeer (sorry Santa). And I got to see snow!

Sweden, I’ll be back!

All the best,
Shay.



MVP for Another Year!

Two weeks ago I received the email from Microsoft, notifying me that I’m a Microsoft C# MVP for another year!

MVP!
I’m very excited that I get the opportunity to continue and affect future Microsoft releases. Thanks CodeValue, Microsoft Israel and everyone else for the support!

I’d also like to congratulate the other renewed MVPs and the new addition to the list of Israeli MVPs – Shlomo Glodberg. Mazal Tov Shlomo!

All the best,
Shay.



Slides + Code Samples from my Session at SDC2011

Today I had the honor to talk before the Scandinavian crowd in Gothenburg at SDC2011. My session was “The Big Comparison of ASP.NET MVC View Engines” where I compared ASPX, Razor, Spark, NHaml and the StringTemplate view engines.

Thanks to everyone who attended! I hope you had as much fun as I had!

The code samples from the session can be downloaded from my github account: https://github.com/shayfriedman/AspNetMvcViewEnginesSamples

And here are the slides:

A recording of the session should be available on the conference site in a few days.

All the best,
Shay.



My Session at mvcConf 2–The Big Comparison of ASP.NET MVC View Engines

About a month ago I gave a session at the virtual mvcConf 2 event. My session was “The Big Comparison of ASP.NET MVC View Engines” where I talked about the differences between ASPX, NHaml, Spark and the Razor view engines.

The session was recorded and it is available on channel 9: http://channel9.msdn.com/Series/mvcConf/mvcConf-2-Shay-Friedman-The-Big-Comparison-of-ASPNET-MVC-View-Engines (at the time of this writing, it has more than 7,500 views! amazing!)

During the session I held a few polls about the preferences of the audience… the results were kinda interesting. These are screenshots of the results of the individual votes:

mvcConf 2 - The Big Comparison of ASP.NET MVC View Engines Poll #1mvcConf 2 - The Big Comparison of ASP.NET MVC View Engines Poll #2mvcConf 2 - The Big Comparison of ASP.NET MVC View Engines Poll #3

And summing all of them together, Razor got 70% of the votes, ASPX got 13%, Spark 12% and NHaml ended up with 6%:

image

So in conclusion, my prediction is that razor gonna conquer this asp.net mvc view engine market… However, the others will stay relevant and I recommend you to check them out. If they make you happier and more productive then go with them! do NOT hesitate.

Anyway, during the session I promised to post a detailed list of resources about the different view engines – so here it is:

Thanks to all the people who made mvcConf happen! and thanks to all attendees!

All the best,
Shay.



mvcConf, SDC, MIX11 Voting and More

2010 was a great year for me in terms of public speaking and at the moment I’m working on making this year even better!

MIX11 is 2 months away and I wanna be there and tell you about writing ASP.NET MVC view engines! But for that I need your help - please vote for my session at http://live.visitmix.com/OpenCall/Vote/Session/55 (the voting ends tomorrow, on Feb 4th). You’ll benefit two things from doing that – you’ll be able to learn about writing ASP.NET MVC view engines at MIX and you’ll enable me to visit Las Vegas after 14 years of absence! Smile

Apart from MIX, I’m booked for a few events at the moment already (and more are coming soon, hopefully):

  • February 8th – mvcConf 2 – this is a big virtual event about ASP.NET MVC. My session is “The Big Comparison of ASP.NET MVC View Engines” where I’m going to compare the ASPX, Razor, NHaml and Spark view engines.
  • April 4th-5th, SDC 2011 – a conference which takes place at Gothenburg, Sweden. My session has the same subject as the mvcConf one, “The Big Comparison of ASP.NET MVC View Engines” but I plan to modify it a bit since it’s a live presentation this time.
  • April 6th, Gothenburg’s .NET UG – the exact content for that UG meeting is still being planned. More details will be published closer to the date.

I’m excited about the opportunities and about the possibility to meet some of you in person,
Looking forward to it!
Shay.



Spaghetti Hot Dog

Warning: this is NOT a technical post. At all.

A few days ago I stumbled upon this picture:

166828_483573759365_738209365_6049690_1312232_n

I can’t find the tweet I got it from so if you know its origin, please let me know and I’ll add it here!

So I saw that and it hit me instantly – this is THE BEST IDEA EVER!!!!! I had to try it myself. And I’m proud to introduce…. my own Spaghetti Hot Dogs!

Step 1 – Buy Cheap Spaghetti and Hot Dogs

DSCF0277 (Small)

Step 2 – Boil the Hot Dogs a bit to Soften Them

DSCF0278 (Small)

Step 3 – Insert Spaghetti Sticks into the Hot Dogs

DSCF0283 (Small)

DSCF0285 (Small)

DSCF0289 (Small)

DSCF0290 (Small)

Step 5 – Boil Everything Together

DSCF0296 (Small)

Step 6 – Bon Appétit!

DSCF0299 (Small)

 

DSCF0302 (Small)

It was fun to make and fun to eat! I LIKE!

All the best,
Shay.



Developing Excel Functions in C# for Excel 2010 64-Bit

Lately I needed to write a small “add-in” for Excel that adds a function written in C# (another case of “when your 4 years of office add-in development experience comes back to haunt you”). Before going ahead with that I searched the web for a quick how-to and ran into this old but still relevant blog post by Eric Carter.

I did everything Eric had described, but every time I tried to add the add-in through the Automation Servers dialog I just couldn’t find it in the list. Trying to locate the file through the Browse button resulted in the next window:

The file you selected does not contain a new Automation Server, or you do not hace sufficient privileges to register the Automation Server.

Such a sweet little message that tells you so much and at the same time tells you nothing at all. How can’t you love developing for Office?

After a couple of hours fighting it with everything I knew (run as admin, installing through a setup project, rewriting everything and more) just when I was ready to throw the laptop out the window and wait for Chrome OS to come out, the brilliant mind of Eran Stiller came up with the question – “maybe it’s related to some 64-32 bit stuff”.

Facepalm. Head-to-desk. Facepalm again. Of course it was related!

As you can understand, I have Excel 2010 64bit installed on my machine. And that’s the root of all my problems. Yes, each and every single one of them.
Anyway, To get the addin working on my machine I had to manually register the DLL using the .NET framework’s 64bit regasm utility. If you also have the same problem, follow the next steps to solve it:

  1. Follow all the steps in Eric Carter’s blog post.
  2. Open the project’s properties and uncheck the “Register for COM Interop” checkbox (I actually don’t think it really matters but just in case).
  3. Rebuild the project.
  4. Open cmd (open Start->Run, type “cmd” and hit Enter).
  5. Navigate to %windir%\Microsoft.NET\Framework64\v4.0.30319
  6. Run: regasm /codebase <path_to_dll>\MyAssembly.dll
    Pay attention to the /codebase switch – without it, it won’t work!

That’s it, you’re done. Your command prompt would look similar to that:

regasming the assembly

[Notice that you’ll get a warning here that the assembly is not signed. If you’re writing a real product, make sure you sign it!]

After that, open Excel and add your DLL again (according to the instructions in Eric’s post). After you do that, try using your functions!

I’m adding my sample project here (which works for me) and its output assembly so you can play with it:

Project (5Kb)
Assembly (4Kb)

Let me know if something isn’t clear or doesn’t work for you.

All the best,
Shay.