Create an Project in Visual Studio
-
This is part of my blog series Creating an App in Azure.
In this step, we will open Visual Studio for the first time and start creating an ASP.NET Core Web App. We are going to retrieve data from our Azure Table we created in a previous step using Azure Credentials. Because we are using the Model-View-Controller (MVC) concept, we will create several different components that will all work together to display, create, edit, and delete rows of data. For this example, I will be using Visual Studio 2022 Community Edition and .NET 9. You can also use Visual Studio Code to build an ASP.NET site, but the full Visual Studio is now free and has more features. I will also be using GitHub, but I will handle that in a different blog.
Create your project
- If you haven't already, establish a place to store projects on your computer. For example, I use "D:\Projects".
- Open Visual Studio.
- Under "Get started", click "Create a new project".
- From "Create a new project", under Search type "ASP.NET Core Web" and select "ASP.NET Core Web App (Model-View-Controller)".
- From "Configure your new project", set the Location to your project folder. In our example, "D:\Projects".
- Enter the Project name. In our example, "Tasks".
- Since we are only going to have a single project in our solution, check "Place solution and project in the same directory".
- Verify you are prompted with "Project will be created in ""D:\Projects\Speakers" and click "Next".
- From "Additional information", under Framework choose .NET 9.0.
- Set Authentication type to "Microsoft identity platform". This will add a prompt to sign in to your application via Azure, but also allow users to view your site WITHOUT signing in. For example, you will be able to add and delete tasks, but a visitor will only be able to read them. Sorting out this step has been the most difficult part of my journey, with a lot of setbacks. There are a lot of ways to handle authentication, this is the least complicated I have found.
- Click "Create".
Microsoft identity platform
- From the Microsoft identity platform, click on the application that you will use to authenticate. It should be the App Service you created in a previous step, or a Service Principal (Enterprise Application) you have already configured. In another blog post, I cover granting permissions for this identity to your Storage Account. Click "Next".
- From Configuring Speakers, click "Next".
- From "Summary of changes", click "Finish".
- When the progress is completed, click "Close". If the initial attempt fails, click back through the steps and click "Next" again at each one to succeed.
At this point, we would create a Git repository. We will leave that for another blog post.
Initial Build
- From the Solution Explorer, expand Controllers and click "Home Controller.cs".
- Note that there are two IActionResult methods, Index() and Privacy(). By default, these two methods and views are created for you. This blog is not going into all the details of MVC, but here is a short summary:
- Model: The data layer. In our example, we will simply have a couple of .cs classes, "Models", that will speak directly to Azure Tables. If you want to abstract further, you can move your data layer even lower and the Models would be empty classes that call the data layer. But we are keeping this simple. Just remember this, build your code so that if you change the data source, say to a SQL database, you can replace the Models without changing anything else in the application. You are always returning a Model back to the Controller.
- View: The UI layer. The view is what the user sees, basically the web pages. Views have a .cshtml extension which allows us to imbed C# code as well as JavaScript in our HTML.
- Controller: The managing layer. Think of the controller as a traffic cop, he calls the models and passes the data on to the view. Events that fire in the view (like clicking a link) are handled by the controller, who then returns a result to the view.
- Note the Index() method has no code yet, and simply returns the View(). MVC uses a hierarchy for navigation, first the controller (Home by default) then the view (Index by default). So to navigate to the Privacy view, you would use Home | Privacy. If you had a Test controller, Test would return Test | Index. Every controller has it's own folder, so you can have multiple Index.cshtml files in the same website.
- Right-click on Index. If you were creating a new action, you would click "Add View..." and Visual Studio would create the view with the model information automatically. In this case, simply click "Go To View" to open the Index.cshtml file. NOTE: You can also use Solution Explorer, expand Views, then Home, and click on Index.cshtml to open the file.
- From the Index page, you see there is an h1 tag that says "Welcome". Edit the welcome line to something else so you can see the results, like "Welcome to my new app".
- Click F5 to compile your application and open it in debug mode in your default browser. Note you are prompted to sign in with a Microsoft account.
- Verify your website is displayed. Click between "Home" and "Privacy" to see the two views.
- From Visual Studio, click Shift-F5 or the red "Stop Debugging" icon in the Ribbon. Note that your browser window will remain open, but if you try to navigate, you will get an error as the temporary web hosting has stopped.
Anonymous Access
- Click on the HomeController.cs file. Note that at the top of the class definition is the attribute [Authorize]. There are two attributes available when you allow anonymous access to a website, [Authorize] and [AllowAnonymous]. Add the attribute [AllowAnonymous] before Index() and [Authorize] before Privacy().
- From Solution Explorer, click on "Program.cs". Locate app.UseAuthorization(); and just above it, enter app.UseAuthentication();
- From Solution Explorer, expand Views, expand Shared, and click on "_Layout.cshtml". Layouts is the base class for all views. HTML and code entered here will be displayed on all views within your application. Thus, the header and footer of each page. It is also where you include stylesheets and libraries to be used by all views.
- From the Layout view, locate the asp-actions "Index" and "Privacy". Note they are within two <li> tags, within an unordered list <ul>.
- Place your cursor before the second <li> tag and enter the following code:
@if (User.Identity?.IsAuthenticated == true)
{ - Place your cursor after the second </li> tag and enter a closing curly bracket:
} - Click F5 to compile and launch your application (note that all edited files are automatically saved).
- Note that you are signed in, and your name appears to the right of the navigation, next to a link to "Sign out". Also note that Home and Privacy are both displayed.
- Click "Sign out". Follow the prompts to sign out.
- Now you should see your website again, and "Signed out" is displayed on the page. Also note that Privacy is no longer displayed. Click "Home".
- Click in the URL after localhost and the port number (example: localhost/7189) and enter "/Home/Privacy" and click Enter. This is the URL of the Privacy view, which does NOT allow anonymous access.
- Note you are prompted to sign in again. Complete the sign-in process.
- Note you can now see the Privacy page again. We should point out there is nothing special about the Privacy page, we simply used it to demonstrate how to accommodate both private actions (signed in) and anonymous actions (not signed in) on the same website. A warning, be sure that you attach [Authorize] and [AllowAnonymous] to classes and methods in the controller, as well the code above to hide buttons and links in the view. You need to both hide the ability to execute an action, as well as the link to that action, so users are not confused. Don't show a link if the user can't use it.
NuGet Packages
In order to work with the Azure Table, we must add a library to our application. The modern version of Visual Studio has simplified this process, you only add the libraries you need.
- In Visual Studio, from Solution Explorer, right-click on your project and click "Manage NuGet Packages..."
- From the NuGet Package Manager, click Browse and enter "Azure.Data.Tables" in the search bar. Click Azure.Data.Tables and click Install. Click "Accept" when prompted. Now the classes for this namespace are available for you to use.
- You should take note that as each package has a no release, you will be prompted to update the library. Click Updates, select all items, and click "Update" to stay current.
Creating a Project in Visual Studio Part II
- Saturday, September 13, 2025 3:14 AM
- Category
- Keywords
- Private
- Azure
- Development