You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blazor中使用Azure SDK获取FTP端点?

获取Azure App Service的FTP端点(编程方式)

你可以通过以下两种方式获取Azure门户中显示的FTP端点:

方法一:直接构造FTP端点(标准场景适用)

Azure App Service的FTP端点遵循固定格式,可通过应用名称直接拼接:

// 从WebSite资源实例中获取应用名称
string appName = _webSite.Data.Name;
// 构造标准FTP端点
string ftpEndpoint = $"ftp://{appName}.ftp.azurewebsites.net";

如果你的应用使用部署槽,则端点格式为:

string slotName = "your-slot-name"; // 替换为实际部署槽名称
string ftpEndpoint = $"ftp://{appName}-{slotName}.ftp.azurewebsites.net";

方法二:解析发布配置文件XML(更准确可靠)

通过Azure资源管理器API获取包含FTP信息的发布配置文件,解析后提取端点:

using System.Xml.Linq;

// 获取FTP类型的发布配置文件(包含密钥)
var publishingProfileResult = await _webSite.GetSitePublishingProfileXmlWithSecretsAsync(
    WaitUntil.Completed, 
    AppServicePublishingProfileFormat.Ftp
);
var xmlContent = publishingProfileResult.Value;

// 解析XML文档
XDocument doc = XDocument.Parse(xmlContent);
XNamespace ns = "http://schemas.microsoft.com/developer/2009/04/serialization/";

// 筛选出FTP类型的发布配置项
var ftpPublishProfile = doc.Descendants(ns + "publishProfile")
    .FirstOrDefault(p => p.Attribute("publishMethod")?.Value.Equals("FTP", StringComparison.OrdinalIgnoreCase) == true);

if (ftpPublishProfile != null)
{
    // 提取FTP端点、用户名、密码
    string ftpEndpoint = ftpPublishProfile.Attribute("publishUrl")?.Value;
    string ftpUsername = ftpPublishProfile.Attribute("userName")?.Value;
    string ftpPassword = ftpPublishProfile.Attribute("userPWD")?.Value;
    
    // 使用这些信息初始化WinSCP会话
}

这种方式能准确获取Azure门户中显示的FTP端点,即使应用有自定义FTP配置或Azure调整端点格式,也能保证兼容性。

内容的提问来源于stack exchange,提问作者Adnan Hodzic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 20:36:06