Getting Started with .Net Core MVC in Ubuntu

Microsoft has been working hard to bring .Net (Core) to Linux so lets explore the fruits of their labor.

Why?

Personally I’ve grown fond of C# and  I love that the new Microsoft is embracing Linux and it doesn’t hurt that it’s starting to perform better than the competitors out there like Node.js. I also personally like that C# syntax is close to PHP, which made it easy for me to learn, and that Visual Studio Code/Community “Intellisense” (code autocomplete if you will) makes writing C# fun and relatively easy. Lastly I like the fact that C# is compiled code and not a scripting language and that it can be written into everything from a console application to a Web Application.

Need more reasons?

Prerequisites

First lets go over some things you should have to start this:

  • Visual Studio Code (Download Link)
https://code.visualstudio.com
  • NPM (Terminal command)
sudo apt-get install npm
  • Update NPM version to latest (Terminal command)
sudo npm install -g npm
  • Install Node.js (Terminal command)
sudo apt-get install nodejs-legacy
  • Install Yeoman (Terminal command)
sudo npm install -g yo
  • Install ASP.Net Generator (Terminal command)
sudo npm install -g generator-aspnet
  • Install Bower
sudo npm install -g bower
  • Install .Net Core SDK
Instructions: https://www.microsoft.com/net/core
  • Some of the Microsoft Yeoman templates now use “old” .Net Core versions. This will fix those dependencies by installing all .Net Core Framework versions.
sudo apt-get install dotnet-dev*

Getting Started

That should cover the pre-requisites needed but if I missed anything please comment below. Now lets start our Web App.

mkdir mywebapp

cd mywebapp

yo aspnet

Note: If you get this error ” permission denied ‘/home/user/.config/configstore/insight-yo.yml’ You don’t have access to this file.

Then try this fix (I received this error on Ubuntu 17.04.):

sudo chmod -R 777 ~/.config/".

You should now have a screen like this one:

yo aspnet

Lets Choose “Web Application” (down arrow twice) and hit enter.

bootstrapNow lets choose Bootstrap for our UI framework

webappname

Type a name and hit enter (I choose mywebapp again).

postinstallinstructions

At this point we can follow the instructions printed above in green and run our web app.

dotnetrestore

cd mywebapp

dotnet restore

dotnet run

dotnetrunning

At this point your new web application should be compiled and running locally on port 5000

http://localhost:5000

chromewebapprunning

Visual Studio Code

Now that you have an application framework downloaded and running it’s a great time to fire up Visual Studio Code and take a look at the source code.

So open Visual Studio and go to file -> Open Folder and choose mywebapp/mywebapp under your home folder or wherever you ran mkdir in your terminal earlier. It should look like this:

vscode

From here I’m not going to go over creating an MVC application because that has already been done very well in other places.

For the basics on ASP.Net MVC

I would start here:

The previously suggested videos above are from when I learned MVC on .Net before .Net Core but now that I’m learning more I’m finding that much has changed so I would recommend going here instead:

This post was based largely on this ASP.Net Core tutorial for OSX and modified for Ubuntu.

Thank You

Thanks for reading. If you found any of this useful please share this page and if there’s any questions or comments please add them below and I’ll be happy to respond when I can.

5 Comments

  1. Mattias Örtenblad

    Nice Post! I’m gonna try this

  2. Davros

    Hi – following your tutorial (with prerequisites) … as I understand it currently (in limited fashion) … I think you need nodejs-legacy to precede Yoeman (Yoeman seems to look for ‘node’ and fails for me unless I first install nodejs-legacy). Ubuntu 16.04 Lts. I’ve already installed Visual Studio Code and .Net Core. Looking like a good tutorial, ta.

  3. [email protected]

    I think you need even npm install -g bower

  4. andyrblank

    @[email protected] I ran this tutorial again with a clean install of Ubuntu 17.04 and it’s not a dependency for what is covered here but certainly all of the front-end scripts in .Net core come from bower so it will be a requirement for further development.

  5. andyrblank

    @Davros you are correct. Thank you! I was also able to fix that error by a permissions fix (sudo chmod -R 777 ~/.config/) but it is better just to fix the order.

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2023 Blank's Tech Blog

Theme by Anders NorenUp ↑