Visual Studio Core for Web and Desktop Apps

Author

Reads 333

Man Working on Computers Coding
Credit: pexels.com, Man Working on Computers Coding

Visual Studio Core is a game-changer for developers, offering a streamlined and efficient way to build, debug, and deploy web and desktop applications.

With Visual Studio Core, you can create web applications using ASP.NET Core, a cross-platform framework that allows you to build web applications using C# or F#.

The Core framework provides a lightweight and modular way to build web applications, making it easy to get started and scale your project as needed.

A unique perspective: Visual Studio Web Programming

Getting Started

To get started with Visual Studio Core, you'll first need to create a project. You can do this by going to the Explorer view and selecting Create .NET Project, or by using the Command Palette and typing ".NET" to find the New Project command.

To create a .NET console app project, choose Console App as the project template, and then select the location where you'd like the new project to be created. Give your new project a name, such as "HelloWorld", and select to show all template options.

Woman in stylish outfit getting ready to go out from the bedroom.
Credit: pexels.com, Woman in stylish outfit getting ready to go out from the bedroom.

Here's a step-by-step guide to creating a .NET console app project:

  1. Start Visual Studio Code.
  2. Go to the Explorer view and select Create .NET Project.
  3. Choose Console App as the project template.
  4. Give your new project a name, such as "HelloWorld".
  5. Set Do not use top-level statements to true.
  6. Open the Program.cs file to see the simple application created by the template.

Once you've created your project, you can start it by pressing F5 or selecting the Start button at the top of the window. This will launch the app, and you can check the console output for messages.

Create a New App

To create a new app, start by opening Visual Studio and selecting Create a new project. You can also use Visual Studio Code, which can be opened by typing "Visual Studio Code" in your computer's search bar.

In Visual Studio, select C# from the Language list and Windows from the platform list, then choose the ASP.NET Core Web App template. This will give you all the template files you need to build a fully functional website.

When creating a new project in Visual Studio Code, select the .NET console app project template and name your project "HelloWorld". You can also choose to create a web project by selecting the ASP.NET Core Web API template.

Credit: youtube.com, How to Make an App for Beginners (2020) - Lesson 1

To configure your new project, enter the project name in the Project name field and select the Target Framework. For example, in the ASP.NET Core Web App (Razor Pages) template, the Target Framework is .NET 8.0.

Here are the steps to create a new project in Visual Studio:

1. Select Create a new project.

2. Choose C# from the Language list and Windows from the platform list.

3. Select the ASP.NET Core Web App template.

4. Enter the project name and select the Target Framework.

5. Select Create.

Similarly, here are the steps to create a new project in Visual Studio Code:

1. Select Create .NET Project.

2. Choose Console App.

3. Select the location where you would like the new project to be created.

4. Name your project "HelloWorld".

5. Select to Show all template options.

6. Set Do not use top-level statements to true.

7. Select Create Project.

Whether you choose to create a console app or a web project, the process is relatively straightforward and can be completed in just a few steps.

Tour Your Solution

A man wearing headphones works on a computer with two monitors in a modern office setup.
Credit: pexels.com, A man wearing headphones works on a computer with two monitors in a modern office setup.

Let's take a tour of your solution and see what's inside. The project template creates a solution with a single ASP.NET Core project named MyCoreApp.

The Solution Explorer tab is a great place to view the contents of your project. Expand the Pages folder to see the files that make up the pages of your website.

Select the Index.cshtml file and view it in the code editor. Each .cshtml file has an associated code file, which is opened by expanding the Index.cshtml node in Solution Explorer and selecting the Index.cshtml.cs file.

The Index.cshtml.cs file contains the code-behind for the Index.cshtml page. You can view this file in the code editor to see how it's implemented.

The project also contains a wwwroot folder, which is the root for your website. You can put static site content such as CSS, images, and JavaScript libraries directly in the paths where you want them.

A fresh viewpoint: Editor Html Visual

Woman in focus working on software development remotely on laptop indoors.
Credit: pexels.com, Woman in focus working on software development remotely on laptop indoors.

The project contains configuration files that manage the web app at run time. The default application configuration is stored in appsettings.json, but you can override these settings by using appsettings.Development.json.

To view the appsettings.json file, simply expand it in the Solution Explorer. This will show you the appsettings.Development.json file, which you can use to override the default settings.

Run the App

To run the app, select Run > Run without Debugging in the upper menu, or use the keyboard shortcut (Ctrl+F5).

Pressing F5 or selecting the Start button at the top of the window is another way to start the app, but in this case, we're focusing on running it.

The program displays "Hello, World!" and ends, giving you a quick glimpse of what's to come.

You can also run the Angular CLI by typing ng start, which will start the Angular app and populate it with data from the API.

Credit: youtube.com, Mobile Development with Visual Studio 2022(Getting Started)

If you're using the ASP.NET Core API project, you'll see two command prompts appear: the API project running and the Angular CLI running the ng start command.

Just keep an eye on the console output for any messages, such as an update to Node.js.

If everything goes smoothly, the Angular app will appear in your browser, populated with data from the API.

Related reading: Azure Ai Studio Api

Development Basics

Visual Studio Core is built on top of the .NET Core framework, which provides a cross-platform and open-source alternative to the traditional .NET Framework. This allows developers to build and deploy applications on multiple platforms, including Windows, macOS, and Linux.

The .NET Core framework includes a set of libraries and tools that simplify the development process, such as the NuGet package manager and the Roslyn compiler. These tools make it easier to manage dependencies, compile code, and debug applications.

Developers can create and manage projects using the dotnet command-line interface, which provides a flexible and efficient way to build, run, and package applications.

IDE Basics

Credit: youtube.com, IDE Basics Explained | Features, Types & How IDEs Make You a Better Coder - Ep 3

The Visual Studio IDE is a powerful tool that brings together various features to help you develop apps and learn languages. You can download and install the latest version of Visual Studio for free, and it's perfect for learning and individual use.

To get started, you'll want to check out the short video that introduces you to the IDE and shows you how to use it for basic tasks. This will give you a solid foundation to build on.

The IDE is highly customizable, and you can personalize it to suit your development style and requirements. You can change the theme to dark, adjust the layout of windows, and even set custom keyboard shortcuts.

Visual Studio offers several versions, including Community 2022, Professional 2022, Enterprise 2022, and Insiders 2026. You can choose the version that best suits your needs and save installation time and disk space by selecting only the components needed.

Credit: youtube.com, Back to basics: Everything about Integrated development enviroment (IDE)

Here are the different versions of Visual Studio available:

By customizing your IDE, you can boost your productivity and make development more enjoyable.

Enhance The App

To enhance the app, you need to open the Program.cs file. This is where the magic happens, and you can add some exciting features to your app.

Replace the contents of the Main method in Program.cs with the following code: Console.WriteLine("What is your name?"); var name = Console.ReadLine(); var currentDate = DateTime.Now; Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!"); Console.Write($"{Environment.NewLine}Press Enter to exit..."); Console.Read();

This code displays a prompt in the console window and waits until the user enters a string followed by the Enter key. It stores this string in a variable named name. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named currentDate. And it displays these values in the console window.

IT professional working on a computer in a modern office setting, focused on coding and tasks.
Credit: pexels.com, IT professional working on a computer in a modern office setting, focused on coding and tasks.

To save changes in Visual Studio Code, you have to explicitly save them. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

Select Run>Run without debugging to run the app. You'll see a prompt in the console window asking for your name. Respond to the prompt by entering a name and pressing the Enter key.

Install NuGet Package

To install a NuGet package, you need to add it to your project.

You can do this by selecting NuGet Package Manager > Manage NuGet Packages for Solution from the Tools menu.

Then, select the Browse tab and enter the package name in the search box.

For this tutorial, you'll need to install Microsoft.EntityFrameworkCore.InMemory.

Select the Project checkbox in the right pane and then select Install.

Alternatively, you can use the terminal to install the package.

Open the integrated terminal and change directories to the folder that will contain the project folder.

Credit: youtube.com, Introduction to .NET Package Management using NuGet [Pt 1] | NuGet for Beginners

Run the command `dotnet add package Microsoft.EntityFrameworkCore.InMemory` to install the package.

Visual Studio Code might display a dialog box asking if you trust the authors of the files in the parent folder.

If you trust the files, select Trust the authors of all files in the parent folder.

You can also select Yes, I trust the authors since the project folder has files generated by .NET.

Visual Studio Code will then add a .vscode folder with generated launch.json and tasks.json files.

For guidance on adding packages to .NET apps, see the articles under Install and manage packages at Package consumption workflow (NuGet documentation).

Here are the steps to install a NuGet package in different IDEs:

  • Visual Studio: Select NuGet Package Manager > Manage NuGet Packages for Solution from the Tools menu.
  • Visual Studio Code / Visual Studio for Mac: Open the integrated terminal and run the command `dotnet add package Microsoft.EntityFrameworkCore.InMemory`.

Desktop App Development

With Visual Studio Core, you can create desktop apps using .NET Core.

You can start by learning from the .NET Core 101 video playlist, which will give you a solid foundation in desktop app development.

Desktop app development in Visual Studio Core allows you to build cross-platform apps that can run on Windows, macOS, and Linux.

You can use .NET Core to build desktop apps that are fast, scalable, and secure.

Prerequisites and Setup

Credit: youtube.com, C# .NET Hello World Program in Visual Studio | Prerequisites + Step-by-Step Guide for Beginners

To get started with Visual Studio Core, you'll need to meet some prerequisites. You'll need Visual Studio 2022 with the ASP.NET and web development workload installed.

You'll also need Visual Studio Code and C# for Visual Studio Code (the latest version). Additionally, you'll need the .NET 8.0 SDK. If you're on Linux, you'll need to add the .NET tools directory to your system path using the following command.

Here are the specific tools and software you'll need to install:

  • Visual Studio 2022 with the ASP.NET and web development workload
  • Visual Studio Code
  • C# for Visual Studio Code (latest version)
  • .NET 8.0 SDK

Note that you can follow these instructions on macOS, Linux, or Windows, and with any code editor.

Git/GitHub Tooling

To get started with Git and GitHub, Visual Studio provides a built-in experience that makes it easy to manage your source control, eliminating the need for external tools.

You can authenticate, clone repositories, and create new ones directly within Visual Studio, no Git expertise required.

Visual Studio's built-in Git and GitHub features save you time and hassle, allowing you to focus on your development work.

DevOps practices can be implemented with Azure DevOps Services, enabling continuous building and releasing of apps in the cloud.

Azure DevOps Services also supports Agile practices, helping you streamline your development workflow.

Readers also liked: Azure Core Services

Prerequisites

Credit: youtube.com, MDT Lab Setup - Video 0: Prerequisites

To start building your ASP.NET Core project, you'll need to meet the prerequisites.

You'll need to have Visual Studio 2022 installed with the ASP.NET and web development workload.

To develop with C# in Visual Studio Code, you'll also need to install C# for Visual Studio Code and the .NET 8.0 SDK.

If you're using Linux, you'll need to add the .NET tools directory to your system path with the following command.

Here are the specific tools you'll need to install:

  • Visual Studio 2022 with the ASP.NET and web development workload
  • Visual Studio Code
  • C# for Visual Studio Code (latest version)
  • .NET 8.0 SDK

Web App Development

To create a web project in Visual Studio, you'll need to select the ASP.NET Core Web API template from the File menu. This will give you a fully functional website with all the necessary template files.

Selecting the correct template is crucial, so make sure to choose the ASP.NET Core Web API template. You can do this by entering "Web API" in the search box and selecting the template.

Once you've selected the template, name your project in the Configure your new project dialog. For example, you can name it "TodoApi".

Discover more: Google Data Studio Api

Credit: youtube.com, Learn Web Development with .NET and Visual Studio

In the Additional information dialog, you can enable Docker support and add authentication support. However, if you're just starting out, you may want to leave the Enable Docker box unchecked and select None for Authentication Type.

To create a project, you can follow these steps:

  • Select Create a new project from the start window.
  • In the Create a new project window, select C# from the Language list and Web from the project types list.
  • Select the ASP.NET Core Web App template and enter your project name in the Project name field.
  • Verify that the correct target framework is selected, such as .NET Core 3.1 or .NET 8.0.
  • Choose your authentication type and select Create.

Here's a summary of the project creation process:

Database and Models

In Visual Studio Core, a model is a set of classes that represent the data the app manages. The model for this app is the TodoItem class.

You can add a model class by right-clicking the project, selecting Add > New Folder, naming the folder Models, and then adding a class named TodoItem. The Id property functions as the unique key in a relational database.

Model classes can be placed anywhere in the project, but the Models folder is used by convention.

Here's a quick rundown of the steps to add a model class:

  • Right-click the project and select Add > New Folder, naming the folder Models.
  • Right-click the Models folder and select Add > Class, naming the class TodoItem.
  • Replace the template code with the following:

The database context is the main class that coordinates Entity Framework functionality for a data model. To register the database context, you need to add using directives and add the database context to the dependency injection (DI) container.

To add a database context, right-click the Models folder and select Add > Class, naming the class TodoContext and clicking Add.

Curious to learn more? Check out: Add Azure Devops Server to Visual Studio

Add Model Class

Credit: youtube.com, Database : Database Models (08:03)

To add a model class, you can create a new folder named Models in your project. This is where you'll store your model classes, which represent the data your app manages.

The TodoItem class is a good example of a model class. It's a set of classes that represent the data your app manages.

You can add the Models folder by right-clicking your project in Solution Explorer and selecting Add > New Folder. Name the folder Models.

In the Models folder, create a new class named TodoItem. You can do this by right-clicking the Models folder and selecting Add > Class. Name the class TodoItem and select Add.

The Id property in the TodoItem class functions as the unique key in a relational database. This is important for identifying each item uniquely.

Model classes can go anywhere in the project, but the Models folder is used by convention. This makes it easier to find and organize your model classes.

Credit: youtube.com, Creating a Model for an Existing Database in Entity Framework Core

Here's a quick rundown of the steps:

  • Create a new folder named Models in your project.
  • Add a new class named TodoItem to the Models folder.
  • Replace the template code with the following:

In the terminal, you can run the following commands to create the Models folder and TodoItem class:

  1. Control-click the TodoAPI project and select Open in Terminal.
  2. Run the following commands:

Register Database Context

Registering the database context is a crucial step in setting up your ASP.NET Core project. You'll need to add the database context to the dependency injection (DI) container.

To do this, you'll need to add using directives and specify that the database context will use an in-memory database.

Here's a step-by-step guide to registering your database context:

  • Add the using directives for the Microsoft.EntityFrameworkCore namespace.
  • Add the database context to the DI container, specifying that it will use an in-memory database.

This will enable the database context to be used throughout your project, making it easier to manage your data.

By following these steps, you'll be able to register your database context and take the next step in setting up your project.

Controller and Routing

The [ApiController] attribute is used in ASP.NET Core to enable API-specific features and behaviors. This attribute is typically used with the [Route] attribute to define the URL path for each method in the controller.

Credit: youtube.com, Resolving ASP.NET Core Controller and Routing Issues Step-by-Step

The URL path for each method is constructed by replacing the [controller] placeholder in the Route attribute with the name of the controller, minus the "Controller" suffix. For example, the controller class name "TodoItemsController" becomes the controller name "TodoItems".

Here's a step-by-step breakdown of how the URL path is constructed:

  • Start with the template string in the Route attribute, such as [Route("api/[controller]")]
  • Replace [controller] with the name of the controller
  • Append any additional route template specified in the HttpGet attribute, such as [HttpGet("products")] (optional)

For example, if the controller class name is "TodoItemsController", the URL path would be "api/TodoItems".

Scaffold a Controller

Scaffolding a controller is a straightforward process that can be completed in a few steps. Right-click the Controllers folder and select Add > New Scaffolded Item.

To scaffold a controller, you'll need to add NuGet packages required for scaffolding. The scaffolding engine, dotnet-aspnet-codegenerator, will be installed after uninstalling any previous version.

The generated code will mark the class with the [ApiController] attribute, indicating that the controller responds to web API requests. This attribute enables specific behaviors, such as creating web APIs with ASP.NET Core.

Credit: youtube.com, Scaffold Controller & Views

The ASP.NET Core templates for controllers with views include [action] in the route template, while API controllers don't. This means the action name isn't included in the endpoint when the [action] token isn't in the route template.

To scaffold a controller, follow these steps:

  • Right-click the Controllers folder.
  • Select Add > New Scaffolded Item.
  • Select API Controller with actions, using Entity Framework, and then select Add.
  • In the Add API Controller with actions, using Entity Framework dialog, try scaffolding a second time if the operation fails.

The generated code will use Dependency Injection (DI) to inject the database context (TodoContext) into the controller. This database context is used in each of the CRUD methods in the controller.

Routing and URLs

ASP.NET Core routing is case insensitive, meaning it doesn't care about capital letters in the controller name.

The URL path for each method is constructed by replacing [controller] with the name of the controller in the Route attribute. For example, if the controller class name is TodoItemsController, the controller name is "TodoItems".

If the [HttpGet] attribute has a route template, it's appended to the path. However, in this sample, no template is used.

Credit: youtube.com, Simplifying ASP.NET MVC Routing: How to Map URLs Without a Category Parameter

Here's a step-by-step breakdown of how the URL path is constructed:

  • Start with the template string in the controller's Route attribute.
  • Replace [controller] with the name of the controller.
  • Append any route template from the [HttpGet] attribute, if present.

In the GetTodoItem method, "{id}" is a placeholder variable for the unique identifier of the to-do item, which is provided to the method in its id parameter when invoked.

Testing and Deployment

In Visual Studio 2022 version 17.3 or later, you can publish the integrated solution using the Visual Studio Publish tool.

To publish, you'll need to create your JavaScript project using Visual Studio 2022 version 17.3 or later. This will allow you to use the publish feature.

Once you've created your project, right-click the ASP.NET Core project and select Publish to get started. You'll then be able to select options to match your desired publish scenario, such as Azure or publishing to a folder.

The publish process can take more time than a standard ASP.NET Core project, since the npm run build command gets invoked when publishing. If you publish to a folder, be aware that the ASP.NET Core directory structure will be followed.

Verify Port

Hands typing code on a laptop in a workspace. Indoor setting focused on software development.
Credit: pexels.com, Hands typing code on a laptop in a workspace. Indoor setting focused on software development.

To ensure your application is communicating correctly, you may need to verify your port settings.

Go to the launchSettings.json file in your ASP.NET Core project, located in the Properties folder, and get the port number from the applicationUrl property.

If there are multiple applicationUrl properties, look for one using an https endpoint, which should resemble https://localhost:7049.

Update the target property in the proxy.conf.js file for your Angular project, located in the src folder, to match the applicationUrl property in launchSettings.json.

The updated value should look like this: target: 'https://localhost:7049',

Docker and Deployment

If you're creating a project with Docker support enabled in Visual Studio Core, you'll need to update the proxy.conf.js file to match the HTTPS port in the Containers window.

To find the HTTPS port, open the Containers window in Visual Studio and check the Environment or Ports tab. You can also use the launchSettings.json file to add the port if it's missing.

Credit: youtube.com, Get Started with Dev Containers in VS Code

Once you've identified the HTTPS port, update the target variable in the proxy.conf.js file to match it. For example, if the HTTPS port is 62958, change the line `const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` : env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:7209';` to `const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` : env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:62958';`.

Here are the steps in a concise format:

  1. Get the HTTPS port from the Containers window in Visual Studio.
  2. Update the proxy.conf.js file to match the HTTPS port.
  3. Restart the app.

If the HTTPS port is missing in the Containers window, you can add it to the launchSettings.json file by adding the "sslPort" property after the "useSSL": true entry. For example, you can add "sslPort": 62958 to the launchSettings.json file.

Margarita Champlin

Writer

Margarita Champlin is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for simplifying complex topics, she has established herself as a go-to expert in the field of technology. Her writing has been featured in various publications, covering a range of topics, including Azure Monitoring.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.