Introduction to ASP.NET Core part 9: MVC continued with routing

Introduction

In the previous post we continued our exploration of MVC in ASP.NET Core. We discussed how a controller action can return various response types that implement the IActionResult interface. The are many return types to choose from and many of them map to a standard HTTP status code like 200 OK, 201 Created etc. There are other response types such as ObjectResult that serialises the provided object using a serialiser which usually defaults to JSON. Finally we saw how to connect a controller action with a view that accepts an object. The view can use the injected object to render dynamic HTML. The default view file type in .NET Core is a Razor view with the cshtml file extension. We can mix HTML and C#-like syntax in a cshtml file. So we have managed to connect the various parts of the MVC pattern in our demo application: a URL is routed to a controller and an action method, the action method builds a model which is injected into a view and the view uses the model to build the final HTML that’s sent back to the user.

In this post we’ll take another look at routing.

Read more of this post

Flatten sequences with the C# LINQ SelectMany operator

Suppose that we have an object with a collection of other objects, like a customer with order items. Then we can also have a sequence of customers where each customer will have her own list of orders. It happens that we want to analyse all orders regardless of the customer, like how many of product A have been sold. There are several options to collect all orders from all customers and place them into one unified collection for further analysis.

The C# SelectMany operator has been specifically designed to extract collections of objects and flatten those collections into one. This post will provide a couple of examples to demonstrate its usage.

Read more of this post

Introduction to ASP.NET Core part 8: MVC continued with controller actions and our first view

Introduction

In the previous post we started looking into MVC in .NET Core. We added a new library through NuGet and wired up the MVC middleware in Startup.cs. Routing is about mapping parts of the requested URL to a controller and an action. The controller is a normal C# class which most often derives from the Controller base class. The action is a C# function that returns some type of response to the HTTP request. The action can have 0, 1 or more parameters that must match the query parameters of the URL otherwise the routing engine won’t find it. We also wrote our first controller called the HomeController and the first action called Index which corresponds to the /home/index URL. We also discussed the basics of routing and added a very basic convention based route to Startup.cs.

Read more of this post

Resolving null values in C#

Say you have a method which accepts a string parameter. The method may need to handle null values in some way. One strategy is to validate the parameter and throw an exception:

private static string Resolve(string input)
{
	if (input == null) throw new ArgumentNullException("Input");
.
.
.
}

Another strategy is to provide some default value with an if-else statement:

Read more of this post

Introduction to ASP.NET Core part 7: starting with MVC

Introduction

In the previous post we took a look at how to work with various environments and their settings. With environment we now mean things like the development, alpha, beta, production etc. environments that a typical web application goes through. New things are tested in development, then the changes are deployed to beta/release/staging and when everyone is happy with the features then the application is deployed to the production environment. The most important point in our discussion was that an application will likely have different settings in different environments. A setting such as “RabbitMqServiceUrl” will have a certain value in the alpha environment but it will most definitely differ from the service URL in the production environment. We saw that the various configuration sources behave like cascading style sheets. We can load multiple settings sources in code. The first one will contain the generic values and the subsequent ones the values of a certain environment. The environment specific settings will override the generic ones.

In this post we’ll start looking at how to work with MVC in .NET Core.

Read more of this post

Forward piping in F#

In F# it’s possible to chain function calls and feed the return value of each function to the next using the forward type operator which looks like this: |> . We can literally read it as “pipe forward”.

If we have a function that increments an integer like this…:

let incrementByOne x = x + 1

Then we normally call it as follows:

let res = incrementByOne 10

With forward piping we can supply the input parameter first as follows:

Read more of this post

Partially applied functions in F#

F# has a number of interesting features related to functions. After all it’s a functional language, right? One of those features is called partial application of a function. Partial application means that in F# we can supply the arguments to function at various stages. We don’t have to provide all input parameters at once.

This feature can be explained through function types in F#.

Consider the following function that adds three numbers:

Read more of this post

Introduction to ASP.NET Core part 6: environments and settings

Introduction

In the previous post we looked at how to deal with static files in .NET Core. Examples of static files include JS, CSS files, images and HTML pages such as index.html. They are normally placed within the web root folder called wwwroot in an ASP.NET project although we can configure other folders in code. These static files are not reachable via a URL at first. Navigation needs to be turned on in Startup.cs using specialised middleware. Hence handling static files is a good example of the modularity that .NET Core claims to display. We also saw how to activate the built-in welcome page via another middleware. It is also a static page and its main purpose is to check whether our ASP.NET web application has been correctly started by the web server. We also saw how to serve up a default and a customised home page for the site visitors.

In this post we’ll investigate the various environments available for an ASP.NET Core project.

Read more of this post

Overriding the + and – operators in C# .NET

It’s easy to override the mathemtical operators like + or – in C# to build custom operations.

Consider the following simple Rectangle class:

public class Rectangle
{
	public Rectangle(int width, int height)
	{
		Height = height;
		Width = width;
	}

	public int Width
	{
		get;
		set;
	}

	public int Height
	{
		get;
		set;
	}
}

Read more of this post

Introduction to ASP.NET Core part 5: static files

Introduction

In the previous post we looked at what middleware means and how it works in .NET Core. A piece of middleware is a component that can be installed during the startup phase of the application in the Configure method of Startup.cs. The components make up a pipeline that the web request travels through until it reaches a so-called terminal component. Once the request has been processed by a terminal component it travels back through the same pipeline in the opposite direct in order to provide some answer to the request. We saw a couple of examples of built-in middleware and we also investigated how to build our own custom component. We also built a wrapper around our component so that it could be easily installed as an extension method of the application builder.

Understanding the basics of middleware is a good foundation for many other areas in .NET as a lot of functionality requires the installation of middleware in Startup.cs. The topic of this post is no exception. We’re going to look at how to work with static files.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.