如何通过Microsoft.Azure.Management.ApiManagement用C#导入Swagger到APIM
Great news—you're already halfway there with your authentication setup. To import a Swagger/OpenAPI spec instead of creating an empty API, you just need to leverage the ApiCreateOrUpdateParameter class and populate its content properties, which map directly to the parameters you're using in your PowerShell command.
Step 1: Confirm Dependencies
First, make sure you have the latest version of the Microsoft.Azure.Management.ApiManagement NuGet package installed in your project—this gives you access to all the required models and client methods.
Step 2: Update Your API Creation Method
Here's how to modify your AzureApiManagementService class to handle Swagger imports:
using Microsoft.Azure.Management.ApiManagement; using Microsoft.Azure.Management.ApiManagement.Models; using System.IO; using System.Threading.Tasks; public class AzureApiManagementService { private readonly ApiManagementClient _apiManagementClient; public AzureApiManagementService(string subscriptionId, AzureApiManagementServiceCredentials credentials) { _apiManagementClient = new ApiManagementClient(credentials) { SubscriptionId = subscriptionId }; } public async Task<ApiContract> CreateApiWithSwaggerAsync( string resourceGroupName, string serviceName, string apiId, string apiPath, string swaggerFilePath, string displayName = "Imported Swagger API", string backendServiceUrl = "https://your-backend-endpoint.com") { // Read the raw Swagger JSON content from your file string swaggerContent = await File.ReadAllTextAsync(swaggerFilePath); // Build the API parameter object with Swagger import details var apiCreationParams = new ApiCreateOrUpdateParameter { Path = apiPath, DisplayName = displayName, ServiceUrl = backendServiceUrl, Protocols = new[] { Protocol.Https }, ApiType = ApiType.Http, // Core properties for Swagger import ContentValue = swaggerContent, ContentFormat = ContentFormat.SwaggerJson // Use ContentFormat.OpenApi30 for OpenAPI 3.0 specs }; // Execute the API creation/import operation return await _apiManagementClient.Api.CreateOrUpdateAsync( resourceGroupName, serviceName, apiId, apiCreationParams); } }
Key Details Breakdown
ContentValue: This is the raw JSON string of your Swagger/OpenAPI spec. We're reading it from a file here, but you could also fetch it directly from a URL or generate it programmatically.ContentFormat: Matches theSpecificationFormatparameter from your PowerShell command. Choose the right value based on your spec version:ContentFormat.SwaggerJsonfor Swagger 2.0ContentFormat.OpenApi30for OpenAPI 3.0ContentFormat.OpenApiJsonfor JSON-formatted OpenAPI 3.x specs
- Parameter Parity: We've mapped all your PowerShell parameters (
Path,ApiType,Protocol) directly to theApiCreateOrUpdateParameterproperties to keep consistency with your existing workflow.
How to Use the Method
Call it with your APIM service details like this:
var credentials = new AzureApiManagementServiceCredentials(); var apimService = new AzureApiManagementService("your-subscription-id", credentials); var importedApi = await apimService.CreateApiWithSwaggerAsync( "your-resource-group-name", "your-apim-service-name", "unique-api-identifier", "/your-api-path", @"C:\path\to\your\swagger.json");
This will replicate exactly what your PowerShell command does—importing all operations, schemas, and definitions from your Swagger spec into APIM, instead of creating an empty API shell.
内容的提问来源于stack exchange,提问作者Tarek




