您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure 虚拟网络库Azure Virtual Network libraries for .NET

概述Overview

使用 Azure 虚拟网络服务,可以通过虚拟网络 (VNet) 来安全地相互连接 Azure 资源。The Azure Virtual Network service enables you to securely connect Azure resources to each other with virtual networks (VNets). VNet 是自己的网络在云中的表示形式。A VNet is a representation of your own network in the cloud. 还可将 VNet 相互连接到一起,使连接到任一 VNet 的资源可以相互通信。You can also connect VNets to each other, enabling resources connected to either VNet to communicate with each other.

管理库Management library

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.Management.Network.Fluent

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.Management.Network.Fluent

代码示例Code Example

此示例演示如何创建虚拟网络。This example shows how you can create a virtual network.

/* 
  Include these "using" directives...
  
  using Microsoft.Azure.Management.Network.Fluent;
  using Microsoft.Azure.Management.Network.Fluent.Models;
*/
using (NetworkManagementClient client = new NetworkManagementClient(credentials))
{
    // Define VNet
    VirtualNetworkInner vnet = new VirtualNetworkInner()
    {
        Location = "West US",
        AddressSpace = new AddressSpace()
        {
            AddressPrefixes = new List<string>() { "0.0.0.0/16" }
        },

        DhcpOptions = new DhcpOptions()
        {
            DnsServers = new List<string>() { "1.1.1.1", "1.1.2.4" }
        },

        Subnets = new List<Subnet>()
        {
            new Subnet()
            {
                Name = subnet1Name,
                AddressPrefix = "1.0.1.0/24",
            },
            new Subnet()
            {
                Name = subnet2Name,
               AddressPrefix = "1.0.2.0/24",
            }
        }
    };
    
    await client.VirtualNetworks.CreateOrUpdateAsync(resourceGroupName, vNetName, vnet);
}

示例Samples

详细了解可在应用中使用的 .NET 示例代码Explore more .NET sample code that you can use in your apps.