Articles → MICROSOFT AZURE → Configure App Service With Virtual Networks In Azure

Configure App Service With Virtual Networks In Azure






Scenario




  1. Access the database server using the public IP address
  2. Configure the virtual network in the app service and access the database server using the private IP



Implementation




  1. Create an app service
  2. Create a virtual network
  3. Configure the virtual network on the app service
  4. Create a virtual machine to host the database server
  5. Create a database and tables
  6. Disassociate public IP address from the virtual machine
  7. Write code to access the database
  8. Publish app service



Create An App Service




Picture showing a screen for creating the web app in Azure
Click to Enlarge


Create A Virtual Network




Picture showing a virtual network with 2 subnets
Click to Enlarge


Configure The Virtual Network On The App Service




Picture showing the click here to configure link to configure virtual network on the app service
Click to Enlarge



Picture showing the Add Vnet button for adding the virtual network to the app service
Click to Enlarge



Picture showing a screen for adding virtual network
Click to Enlarge


Create A Virtual Machine To Host The Database Server




Picture showing selecting the virtual machine as the database server
Click to Enlarge



Picture showing selecting the virtual network and subnet for the virtual machine
Click to Enlarge



Picture showing setting up the credentials for sql server in the virtual machine
Click to Enlarge


Create A Database And Tables




Picture showing the category table with data
Click to Enlarge


Disassociate Public IP Address From The Virtual Machine




Picture showing a screen for disassociating public IP address from the virtual machine
Click to Enlarge


Write Code To Access The Database






<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
	<div class="jumbotron">
		<h1>ASP.NET</h1>
		<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p>
		<p>
			<a href="http://www.asp.net" class="btn btn-primary btn-lg">Learn more »</a>
		</p>
	</div>
	<asp:GridView ID="gvData" runat="server"></asp:GridView>
</asp:Content>




using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

namespace WebApplication1 {
  public partial class _Default: Page {
    protected void Page_Load(object sender, EventArgs e) {
      if (!IsPostBack) {
        string connectionString = "Data Source=10.0.1.4;Initial Catalog=mydatabase;User Id=demouser;Password=AdminPassword@123";
        DataTable table = new DataTable();
        using(SqlConnection connection = new SqlConnection(connectionString)) {
          using(SqlCommand command = new SqlCommand("select * from category", connection)) {
            using(SqlDataAdapter adapter = new SqlDataAdapter(command)) {
              adapter.Fill(table);

              gvData.DataSource = table;
              gvData.DataBind();
            }
          }
        }
      }
    }
  }
}





Publish App Service




Picture showing the published app service with gridview binded with the data
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, May 14, 2021

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250