Return to site

Visual Studio Core For Mac

broken image


Visual Studio 2019 for Mac version 8.7.6 (8.7.6.2) released September 08, 2020. We updated the.NET Core SDK (3.1.402 SDK and 2.1.22 runtime). Visual Studio 2019 for Mac version 8.7.5 (8.7.5.19) released September 08, 2020. We fixed an issue where an Uno solution could not be loaded. Shell and Tools.

-->

Visual Studio for Mac makes it easy to develop your app's service with its support for the latest ASP.NET Core Web development platform. ASP.NET Core runs on .NET Core, the latest evolution of the .NET Framework and runtime. It's been tuned for fast performance, factored for small install sizes, and reimagined to run on Linux and macOS, as well as Windows.

Installing .NET Core

.NET Core 3.1 is automatically installed when you install Visual Studio for Mac. For more information about versions of .NET Core supported in Visual Studio for Mac see .NET Core Support.

  • Using.NET Core in Visual Studio Code.NET Core provides a fast and modular platform for creating server apps that run on Windows, Linux, and macOS. Use Visual Studio Code with the C# and F# extensions to get a powerful editing experience with C# IntelliSense, F# IntelliSense (smart code completion), and debugging.
  • One place for all extensions for Visual Studio, Azure DevOps Services, Azure DevOps Server and Visual Studio Code. Discover and install extensions and subscriptions to create the dev environment you need.

Creating an ASP.NET Core app in Visual Studio for Mac

Open Visual Studio for Mac. On the Start Screen, select New Project...

This will display the New Project dialog, allowing you to select a template to create your application.

There are a number of projects that will provide you with a pre-built template to start building your ASP.NET Core Application. These are:

  • .NET Core > Empty
  • .NET Core > API
  • .NET Core > Web Application
  • .NET Core > Web Application (Model-View-Controller)
  • .NET Core > Blazor Server App
  • .NET Core > Blazor WebAssembly App

Select the ASP.NET Core Empty Web Application and press Next. Give the Project a Name and press Create. This creates a new ASP.NET Core app. In the solution pad's left pane, expand the second arrow and then select Startup.cs. It should look similar to the image below:

The ASP.NET Core Empty template creates a web application with two default files: Program.cs and Startup.cs, which are explained below. It also creates a Dependencies folder, which contains your project's NuGet package dependencies such as ASP.NET Core, the .NET Core framework, and the MSBuild targets that build the project:

Program.cs

Open and inspect the Program.cs file in your project. Notice that several things are happening in the Main method – the entry into your app:

An ASP.NET Core app creates a web server in its main method by configuring and launching a host via an instance of WebHostBuilder. This builder provides methods to allow the host to be configured. In the template app the following configurations are used:

  • .UseStartup(): Specifies the Startup class.

However, you can also add additional configurations, such as:

  • UseKestrel: Specifies the Kestrel server will be used by the app
  • UseContentRoot(Directory.GetCurrentDirectory()): Uses the web project's root folder as the app's content root when the app is started from this folder
  • .UseIISIntegration(): Specifies that the app should work with IIS. To use IIS with ASP.NET Core both UseKestrel and UseIISIntegration need to be specified.

Startup.cs

The Startup class for your app is specified in the UseStartup() method on the CreateWebHostBuilder. It is in this class that you will specify the request handling pipeline, and where you configure any services.

Open and inspect the Startup.cs file in your project:

This Startup class must always adhere to the following rules:

  • It must always be public
  • It must contain the two public methods: ConfigureServices and Configure

The ConfigureServices method defines the services that will be used by your app.

The Configure allows you to compose your request pipeline using Middleware. These are components used within an ASP.NET application pipeline to handle requests and responses. The HTTP pipeline consists of a number of request delegates, called in sequence. Each delegate can choose to either handle the request itself, or pass it to the next delegate.

You can configure delegates by using the Run,Map, and Use methods on IApplicationBuilder, but the Run method will never call a next delegate and should always be used at the end of your pipeline.

The Configure method of the pre-built template is built to do a few things. First, it configures an exception handling page for use during development. Then, it sends a response to the requesting web page with a simple 'Hello World'.

This simple Hello, World project can run now without any additional code being added. To run the app, you can either select which browser you want to run app the app in using the dropdown right of the Play button, or simply hit the Play (triangular) button to use your default browser:

Visual Studio for Mac uses a random port to launch your web project. To find out what port this is, open the Application Output, which is listed under View > Pads. You should find output similar to that shown below:

Once the project is running, your default web browser should launch and connect to the URL listed in the Application Output. Alternatively, you can open any browser of your choice, and enter http://localhost:5000/, replacing the 5000 with the port that Visual Studio output in the Application Output. You should see the text Hello World!:

Adding a Controller

ASP.NET Core Apps use the Model-View-Controller (MVC) design pattern to provide a logical separation of responsibilities for each part of the app. MVC consists of the following:

Studio
  • Model: A class that represents the data of the app.
  • View: Displays the app's user interface (which is often the model data).
  • Controller: A class which handles browser requests, responds to user input and interaction.

For more information on using MVC refer to Overview of ASP.NET Core MVC guide.

To add a controller, do the following:

  1. Right-click on the Project name and select Add > New Files. Select General > Empty Class, and enter a controller name:

  2. Add the following code to the new controller:

  3. Add the Microsoft.AspNetCore.Mvc dependency to the project by right-clicking the Dependency folder, and selecting Add Package....

  4. Use the Search box to browse the NuGet library for Microsoft.AspNetCore.Mvc, and select Add Package. This may take a few minutes to install and you may be prompted to accept various licenses for the required dependencies:

  5. In the Startup class, remove the app.Run lambda and set the URL routing logic used by MVC to determine which code it should invoke to the following:

    Make sure to remove the app.Run lambda, as this will override the routing logic.

    MVC uses the following format, to determine which code to run:

    /[Controller]/[ActionName]/[Parameters]

    When you add the code snippet above, you are telling the app to default to the HelloWorld Controller, and the Index action method.

  6. Add the services.AddMvc(); call to the ConfigureServices method, as illustrated below:

    You can also pass parameter information from the URL to the controller.

  7. Add another method to your HelloWorldController, as illustrated below:

  8. If you run the app now, it should automatically open your browser:

  9. Try to browse to http://localhost:xxxx/HelloWorld/Xamarin?name=Amy (replacing xxxx with the correct port), you should see the following:

Troubleshooting

If you need to install .NET Core manually on Mac OS 10.12 (Sierra) and higher, do the following:

  1. Before you start installing .NET Core, ensure that you have updated all OS updates to the latest stable version. You can check this by going to the App Store application, and selecting the Updates tab.

  2. Follow the steps listed on the .NET Core site.

Make sure to complete all steps successfully to ensure that .NET Core is installed successfully.

Summary

This guide gave an introduction to ASP.NET Core. It describes what it is, when to use it, and provided information on using it in Visual Studio for Mac.For more information on the next steps from here, refer to the following guides:

  • ASP.NET Core docs.
  • Creating Backend Services for Native Mobile Applications, which shows how to build a REST service using ASP.NET Core for a Xamarin.Forms app.
  • ASP.NET Core hands-on lab.

Related Video

Installation

  1. Download Visual Studio Code for macOS.
  2. Open the browser's download list and locate the downloaded archive.
  3. Select the 'magnifying glass' icon to open the archive in Finder.
  4. Drag Visual Studio Code.app to the Applications folder, making it available in the macOS Launchpad.
  5. Add VS Code to your Dock by right-clicking on the icon to bring up the context menu and choosing Options, Keep in Dock.

Launching from the command line

You can also run VS Code from the terminal by typing 'code' after adding it to the path:

  • Launch VS Code.
  • Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.
  • Restart the terminal for the new $PATH value to take effect. You'll be able to type 'code .' in any folder to start editing files in that folder.

Note: If you still have the old code alias in your .bash_profile (or equivalent) from an early VS Code version, remove it and replace it by executing the Shell Command: Install 'code' command in PATH command.

To manually add VS Code to your path, you can run the following commands:

Start a new terminal to pick up your .bash_profile changes.

Note: The leading slash is required to prevent $PATH from expanding during the concatenation. Remove the leading slash if you want to run the export command directly in a terminal.

Note: Since zsh became the default shell in macOS Catalina, run the following commands to add VS Code to your path:

Touch Bar support

Out of the box VS Code adds actions to navigate in editor history as well as the full Debug tool bar to control the debugger on your Touch Bar:

Mojave privacy protections

After upgrading to macOS Mojave version, you may see dialogs saying 'Visual Studio Code would like to access your {calendar/contacts/photos}.' This is due to the new privacy protections in Mojave and is not specific to VS Code. The same dialogs may be displayed when running other applications as well. The dialog is shown once for each type of personal data and it is fine to choose Don't Allow since VS Code does not need access to those folders. You can read a more detailed explanation in this blog post.

Updates

VS Code ships monthly releases and supports auto-update when a new release is available. If you're prompted by VS Code, accept the newest update and it will get installed (you won't need to do anything else to get the latest bits).

Note: You can disable auto-update if you prefer to update VS Code on your own schedule.

Preferences menu

You can configure VS Code through settings, color themes, and custom keybindings and you will often see mention of the File > Preferences menu group. On a macOS, the Preferences menu group is under Code, not File.

Next steps

Once you have installed VS Code, these topics will help you learn more about VS Code:

  • Additional Components - Learn how to install Git, Node.js, TypeScript, and tools like Yeoman.
  • User Interface - A quick orientation around VS Code.
  • User/Workspace Settings - Learn how to configure VS Code to your preferences settings.

Visual Studio Code For Mac Os

Common questions

Visual Studio Code For Matlab

Why do I see 'Visual Studio Code would like access to your calendar.'

If you are running macOS Mojave version, you may see dialogs saying 'Visual Studio Code would like to access your {calendar/contacts/photos}.' This is due to the new privacy protections in Mojave discussed above. It is fine to choose Don't Allow since VS Code does not need access to those folders.

Visual Studio Code For Macbook Pro

VS Code fails to update

Visual Studio Enterprise For Mac

If VS Code doesn't update once it restarts, it might be set under quarantine by macOS. Follow the steps in this issue for resolution.





broken image