How to link to Controller Actions from Views in ASP.NET MVC Core

22 Mar 2021

Often you need to put links in your ASP.NET MVC Core Views that take users to other controllers in your web app – like navigating to a details page for a particular record in the table. Or when building a primary, secondary or tertiary navigation. Or logging users in or out. Heck, you need links everywhere!

A very obvious (but wrong!) way be hardcoding relative paths to your controller’s actions in the View, like <a href="/product/details/1">View product details</a>. It’s super easy, right? What can possibly go wrong? Actually, a lot of things. Just the tip of the iceberg:

As you can see, hardcoding is a big pain in the rear. But what can you do instead? Fear not, there’s a solution that addresses all the above problems and more!

Read more →

How To Set Default Option In Select List In ASP.NET Core MVC

23 Feb 2021

It can be extremely infuriating to stumble over something so seemingly simple as the Select List control, and having to spend time trying to make it work, instead of focusing on what really matters in the given moment – be it learning a new tech or developing your web app.

ASP.NET Core MVC made is somewhat easier to deal with select lists. Previously in .NET Framework, you were awash with various ways of providing data to your Select Lists, to the point where you realised it was a decoy and the bloody thing just wanted to kill you.

Now a lot of ambiguity seems to have been removed, and Tag Helpers for MVC and Razor Pages are much easier to use. Let’s have a look at how you can set default option for Select List control in ASP.NET Core MVC or Razor Pages.

Read more →

Subscribe now and get helpful tips on developing .NET apps - never miss a new article.

You can unsubscribe at any time. I'll never share your email with anyone else.

How to pass JSON from Controller to View in ASP.NET MVC Core and avoid XSS

10 Feb 2021

When working on your ASP.NET MVC application, you often need to include some of your app’s data as Javascript objects. You may need this for some interactive behaviour, graphs/charts, or simply to “hydrate” the UI with the relevant information, such as username etc.

There’s certainly a big push to move away from rendering JSON data in MVC Views. Instead, it’s recommended to use Ajax calls that fetch JSON data from backend APIs. This helps to separate concerns in your application, making it more maintainable and easier to support, test and debug.

However, sometimes it’s OK to put JSON data directly in MVC Views:

With these caveats in mind, let’s see how you can easily put some JSON into your ASP.NET MVC views.

Read more →

How to test new ideas in your .Net projects quickly and safely with git

05 Nov 2020

Let’s face it – often you just need to get down and dirty, quickly sketch up some code to see if your idea of a bugfix or a feature is going to work.

Sometimes you can whip up some C# code using C# Interactive window in Visual Studio, or an online C# REPL (see this article for more info on that).

But a lot of times you DO need to make changes to your existing project(s), just because of all the other stuff, like DB access, authentication, other APIs responses, routing, dependency injection etc. that is needed to test your brand spanking new idea.

Also, you don’t want to accidentally break already working code and want to be able to roll your changes back quickly if it doesn’t work and you need to pull the plug on it.

Read more →

How to quickly run some C# without a project

01 Oct 2020

Do you ever wish you could just quickly run some C# code, without all the heavy lifting, like creating a new project, setting it up etc? Or worse yet, starting to experiment with some temporary code by modifying an existing solution, and then forgetting to remove your code, and lo and behold, the next thing you know it’s deployed to production?!

Wouldn’t it be nice to be able to play around with any C# code, as if from a command line, but with all the goodies like highlighting and code completion? How awesome it’d be to see all the properties on the objects you’re working with and their actual values, without having to rely on some vague description from MSDN? Think of this as iPython or dev tools Javascript console, but for C#.

That would let you try different ways to solve problems, running code straight away, line by line, without having to restart your project and step into whichever function you’d be modifying instead.

Read more →

How to securely store and retrieve sensitive info in .NET Core apps with Azure Key Vault

17 Sep 2020

If your Web, Console/Windows or WPF app ever needs to talk to databases, other APIs or maybe encrypt/decrypt information it receives from other apps or the user, it needs to access sensitive authentication credentials – such as user names and passwords for database connection strings, API keys, private encryption keys etc.

Where should you store that sensitive info and how would you supply it for your app? The first obvious approach is to store those values in a config file of sorts or a local database. Why not? It’s easy, secrets can be read & used by your app quickly and reliably, it seems.

Well, there are several major issues with that approach:

Read more →

.NET Framework or .NET Core – which one should you learn for web development?

25 Aug 2020

Whether you’re just starting to look into web development after learning some C#, or whether you’re a seasoned Windows developer making forays into web dev, it’s very common to stumble into this massive confusion: it seems like Microsoft deliberately tried to complicate things by creating heaps of technologies and acronyms for them. Let’s have a look, just off the top of my head, we have got the following tech available to us:

Read more →

Online tools for .Net Core and Web development

10 Aug 2020

When you work for long enough on a given project, you kind of get comfortable with the tools and things eventually start to feel familiar. So it’s only natural to start feeling like you’re missing out and there might be newer and greater things out there, some exciting new languages, tools or techniques.

In this article, I’d like to show you some online tools that I found super helpful in my development work.

Read more →

Where should you put "try ... catch" statements in your C# code

17 Feb 2020

Exceptions can happen anywhere. Some are thrown by your code, deliberately. Others are thrown by .NET or 3rd party libraries. You know where some exceptions may get thrown, and there are heaps more you didn’t even know existed.

Most likely you already know how try ... catch ... finally block works (check out this MS doco if you want to brush up on that), and while you understand it all at the high level, you may still be wondering whether you should:

To make an informed judgment, you need to understand the main types of exceptions. This understanding will help you decide when to catch and how to handle individual kind of exception in a given situation.

Read more →

How to call a JSON API and display the result in ASP.NET Core MVC

15 Oct 2019

In today’s world of interconnected apps & services, you may often need to call another API to get or modify information.

In this article I’d like to show you how to do the following:

Let’s get on with it!

Read more →