ASP.NET MVC, Web API & Web Pages released as open source on CodePlex

Posted by Filip Ekberg on March 28 2012 Leave a Comment

Microsoft have just released a lot of code openly on CodePlex.

I don’t think I need to stress how amazing this is!

The source code just released includes the following:

If you head over to the CodePlex page called ASP.NET Web Stack. You’re going to find a lot of useful information here regarding the code base. Since CodePlex now supports git, it’s Very easy for a lot of us to integrate with the development process and contribute patches, yes, you heard me right: contributions by anyone!

To get started, fire up your favorite git program and clone this repository: https://git01.codeplex.com/aspnetwebstack.git.

I’m using SmartGit in Windows and as you can see by the following screenshot, you’ve got a lot of nice information about the commits, branches, merges and fixes! Most important of all, it’s alive, there are a lot of contributions made the last couple of days and I really think this will increase the quality of what is already high quality.

When you’ve got all the code fetched from the repository, all you need to do is fetching all the NuGet packages. They’ve made this process pretty simple, all you need to do is open up a command prompt, go to the folder where your local repository is and write the following:

build RestorePackages
build

Now you’re ready to contribute to ASP.NET MVC/Web API/Web Pages! There are multiple ways to how you can contribute, head over to CodePlex and read more about it.

Vote on HN

Video trailer for A C# Smorgasbord

Posted by Filip Ekberg on March 27 2012 1 Comment

Watch the first video trailer for my upcoming book A C# Smorgasbord.

The work on the book is going great, almost 50% of the work is done and the deadline is approaching, stay tuned for more information, trailers and competitions to win a free copy!

I hope you enjoy it!

Vote on HN

Watch my live stream from #webbdagarna!

Posted by Filip Ekberg on March 22 2012 Leave a Comment

I’m live coding from #webbdagarna, watch me now:

Tweet me if you’ve got any questions or if there are any problems with the stream!

Update 2012-03-25
Thanks to everyone who watched my live stream from Webbdagarna in Stockholm. I’m planning to assemble all the videos from the stream and put them on youtube, I can’t promise that it’s going to work due to youtube/justin.tv limitations.

Vote on HN

Introducing the ASP.NET Web API

Posted by Filip Ekberg on March 19 2012 8 Comments

In the new version of ASP.NET you can use something called ASP.NET Web API. This allows you to expose your data in many different formats, such as XML or JSON. The idea is to provide a REST API where you use HTTP for real. Meaning that you use GET/POST/PUT/DELETE. These work pretty straight forward:

  • GET – Retrieve all or one item
  • POST – Add an item
  • PUT – Update an item
  • DELETE – Remove an item

As mentioned above, you can retrieve data in different formats such as XML or JSON. The type of data that will be in the response is determened by the HTTP header Accept. By default(built-in) you can use the two following accept headers:

  • application/json
  • applicaiton/xml

In order to try this out, I will be using curl to make the web requests, because this will allow me to specify the headers manually.

Start off by creating a new ASP.NET MVC 4 Web Applicaiton in Visual Studio 11 Beta:

Then you will be presented with what kind of ASP.NET MVC 4 project that you want to create, select to create a new Web API project:

When the project is created, you’ll have some new things that you haven’t seen before in a normal ASP.NET MVC application. It does look a lot like a normal ASP.NET MVC applications, but with some minor add-ons.

The first thing that we are presented with is the ValuesController and this controller inherits from the ApiController. The ApiController is what you will inherit from when you are creating API specific controllers. It will help you map the HTTP requests GET/POST/PUT/DELETE to the methods with the corresponding names.

The second thing that is added is inside the global.asax.cs, a new route to specify where we retrieve the API specific controllers:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

This just help us distinguish between our API calls and non-API calls. If you just start the web application and navigate to /api/values/ you will see a list like this:

If we open this in Internet Explorer 10 instead, it will request a JSON result instead of XML and if we open that up in notepad, it looks like this:

We can verify that this works by testing it out with curl.

Get JSON using curl

curl -H "Accept: application/json" -H "Content-Type: application/json" -X GET "http://localhost:13938/api/values"

JSON Result:

["value1","value2"]

Get XML using curl

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET "http://localhost:13938/api/values"

XML Result:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <string>value1</string>
    <string>value2</string>
</ArrayOfString>

This is good and all, but we might want to be able to specify what kind of format that we expect with a query string. To do this, we can register a formatter for a certain query string mapping. Go back to global.asax.cs and add the following at the end of Application_Start:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
    new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));

GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
    new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));

This will allow you to add ?type=json or ?type=xml to request a certain output format:

If you want to retrieve a specific item, you can simply do /api/values/1.

This has been a short introduction to get you started with the ASP.NET Web API. If you found this interesting, stay tuned for the live stream that I will do from Webbdagarna (if the Internet connection allows) and also stay tuned for upcoming posts where we hook up with Entity Framework to make this API more alive.

Vote on HN

Don’t miss me coding on stage for 2 days at Webbdagarna!

Posted by Filip Ekberg on March 17 2012 2 Comments

There’s a Swedish event in Stockholm 22-23 March called Webbdagarna (translation: web days). The focus of the event is to talk about e-commerce, social media, mobile development and much more. It’s not so much programming focus, in fact the only coding part of the event is what me and my colleuge will do.

The idea is that we are going to sit on stage for 2 days right next to the speakers to create a very cool mobile app, live! My main focus on this will be to create the API for the application, I will of course use the latest technology for this:

Our focus is to deliver a proof of concept and not a finished product, but since the audience will be “social media experts”, “seo experts” and whatnot, they won’t be interested in seeing the code. But I know that many of you guys would like to see this so I will be live streaming from the event on JustinTV (if the organizers and bandwidth allows for it).

You can find the stream at justin.tv/fekberg.

I will be at the event to reprecent Star Republic which is where I work as a Software Engineer. We put together a pretty fun YouTube video presentation about what I will do there, I talk Swedish in it. So for you guys that don’t talk Swedish it might be like listening to the Swedish Chef(muppets)!

Video translation: I am Filip Ekberg and I work as a Software Engineer at Star Republic, follow us on this rock-n-roll journey, where we build Swedens coolest app, live on stage at Webbdagarna in Stockholm 22-23 March!

Don’t forget to follow me on twitter, this is where I will announce and communicate with everyone at the event (and outside the event).

Hope you’ll tune in – Rock On!

Vote on HN

Visual Studio 11 Beta

Posted by Filip Ekberg on March 1 2012 2 Comments

Yesterday on February 29, Visual Studio 11 Beta was released and with it comes a beta version of .NET 4.5. You can download Visual Studio 11 Ultimate Beta from Microsofts website.

There has been some changes to the UI that has gotten a lot of attention the last couple of weeks, let’s take a closer look at what we can expect from the Visual Studio 11 Beta.

The first thing that we are faced with is the installer, all the installers for Visual Studio has in my opinion looked much better than any other installers and the installer for Visual Studio 11 Beta is the winner among them. It got a very nice look and feel over it and it’s clear they’ve put some effort into the smallest details.

If you are installing Visual Studio 11 Beta with the Web Installer, it will take some time. Once the installation has finished fire up Visual Studio!

 

When the first screenshots of the Beta was released, many of the Visual Studio users were surprised of the monochrome interface. It’s a quite drastic change compared to what we are used to in Visual Studio. Up until Visual Studio 2010 we’ve been comfortable with a lot of colors and nice icons.

Now, the workspace is in more focus.

The menus, icons, tabs and everything else in the interface just feels right. When you spend 8+ hours per day in Visual Studio, you want something that is easy on the eyes and that is exactly what this is.

Even the icons in the menu have a monochrome approach.

Let us take a look at how it looks and feels to write code, create a new console application.

When the project is created and you’ve got the Program.cs opened add some content to it to get a feeling of the studio.

As you can see the code is in focus, nothing else is stealing your attention. The code coloring is what we are used to and the key mapping as well. Notice the Solution Explorer, there’s an expand button for each file that will let you view classes, methods and properties in the file.

You attention is automatically going towards what is important, the code. Let’s take a look at a last screenshot where it is more clear that the code, comments and breakpoints get your attention.

After you have tried Visual Studio 11, leave a comment and let me know what you think of it!

Vote on HN