Adding C# Scripting to Your Development Arsenal

As part of my efforts to popularize C# scripting in the .NET community (which, hopefully, you have noticed by following this blog, my Twitter or my Github) and in celebration of the first stable release of Roslyn scripting packages on Nuget yesterday, I published a guest post at Canadian Developer Connection blog – Adding C# Scripting to Your Development Arsenal.

You can read the post here, and hopefully you find it useful. It’s part 1 of a short 2-piece series.

Running C# scripts and snippets in Visual Studio Code with scriptcs

Recently Microsoft open sourced the Code editor and I think a lot of people would agree that it’s really awesome. The UI based on Electron and the Monaco editor previously used in Visual Studio Online is really snappy and works great.

Visual Studio Code can also now be extended via plugins – and when that was announced, I thought why not make something for scriptcs? There already is a scriptcs plugin for Sublime Text and for Atom, so it only makes sense to have one for Visual Studio Code.

Continue Reading

Localized routes with ASP.NET 5 and MVC 6

In my Web API book, in one of the chapters (source here), I’m discussing an in interesting approach towards route localization, using attribute routing.

The whole idea came from the fact that at some point in the past I used to work on a really large application – 70+ language versions, all of which required localizations on the route level.

That approach allowed you to define a single attribute route at action level (as opposed to, well, 70+ routes), and have it auto-translated by the plugged in infrastructure, as long as you provide the mapping to other languages at application startup.

Let’s have a look at how same type of functionality can be built in ASP.NET MVC 6.

Continue Reading

Global route prefixes with attribute routing in ASP.NET Web API

As you may have learnt from some of the older posts, I am a big fan, and a big proponent of attribute routing in ASP.NET Web API.

One of the things that is missing out of the box in Web API’s implementation of attribute routing, is the ability to define global prefixes (i.e. a global “api” that would be prepended to every route) – that would allow you to avoid repeating the same part of the URL over and over again on different resources. However, using the available extensibility points, you can provide that functionality yourself.

Let’s have a look.

Continue Reading

Using Roslyn and unit tests to enforce coding guidelines and more

Last year, during a few of my Roslyn talks, I was presenting a cool idea of leveraging Roslyn in unit tests to enforce a certain style in code, and in general inspect the consistency of the code in various ways.

It’s a really powerful concept, and something I wanted to blog about, but of course forgot – until I was reminded of that yesterday on Twitter.

Let’s have a look.

Continue Reading

Running a C# REPL in a DNX application with scriptcs

One of the cool things that scriptcs allows you to do, is that you can embed it into your application and allow execution of C# scripts. There are even some great resources on that out there, like this awesome post by Mads.

The same applies to the REPL functionality – you don’t have to use scriptcs.exe to access the REPL – you can use the scriptcs Nuget packages to create a REPL inside your app.

And because there aren’t that many resources (if any) on how to host a scriptcs REPL, today I wanted to show you just that. But for a more interesting twist, we’ll do that inside a DNX application.

There are many reasons why DNX is awesome, and why you’d want to use it, but especially because, through the project.json project system, it has a much improved way of referencing and loading dependencies and Nuget packages – and we can leverage that mechanism to feed assemblies into our REPL.

Continue Reading

Hacking DNX to run C# scripts

Because of my considerable community involvement in promoting C# scripting (i.e. here or here), I thought the other day, why not attempt to run C# scripts using DNX?

While out of the box, DNX only compiles proper, traditional C# only, thanks to the compilation hooks it exposes, it is possible to intercept the compilation object prior to it being actually emitted, which allows you to do just about anything – including run C# scripts.

Let’s explore more.

Continue Reading

Disposing resources at the end of Web API request

Sometimes you have resources in your code that are implementing IDisposable, and that you’d like them to be disposed only at the end of the HTTP request. I have seen a solution to this problem rolled out by hand in a few code bases in the past – but in fact this feature is already built into Web API, which I don’t think a lot of people are aware of.

Let’s have a quick look.

Continue Reading

ViewComponents in ASP.NET 5 and ASP.NET MVC 6

Let’s have a quick look at another new feature in ASP.NET MVC 6, and that is the ViewComponent feature. View components are intended to be replacements to ChildActions and, to some extent, of partial views.

Traditionally in ASP.NET MVC (and in general in the textbook MVC pattern), you had to compose the entire model in the controller and pass it along to the view, which simply rendered the entire page based on the data from the model. The consequence of this is that the view does not need to explicitly ask for any data – as its sole purpose is to just act upon the model it received.

While this sounds very nice in theory, it has traditionally posed a number of practical difficulties. There are a number of reusable components on pretty much every website – think a menu, a shopping cart, lists of all kinds, breadcrumbs, metadata and so on – so things that appear on multiple pages.

Let’s have a look at how this is solved in MVC 6.

Continue Reading