如何从Azure托管的Web应用程序发布网站?

时间:2015-02-25 06:33:25

标签: azure

需要Azure中托管的网站发布表单Web应用程序的解决方案。

我尝试了以下代码,它创建了域但我无法上传已发布的网站。

private HttpResponseMessage CreateWebsite(CreateSiteViewModel site)         {             var cert = X509Certificate.CreateFromCertFile(Server.MapPath(site.CertPath));             string uri = string.Format(" https://management.core.windows.net/ {0} / services / WebSpaces / {1} / sites /",site.Subscription,site.WebSpaceName);

        // A url which is looking for the right public key with 
        // the incomming https request

        var req = (HttpWebRequest)WebRequest.Create(uri);

        String dataToPost =string.Format(
            @"<Site xmlns=""http://schemas.microsoft.com/windowsazure"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
              <HostNames xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">
                <a:string>{0}.azurewebsites.net</a:string>
              </HostNames>
              <Name>{0}</Name>
              <WebSpaceToCreate>
                <GeoRegion>{1}</GeoRegion>
                <Name>{2}</Name>
                <Plan>VirtualDedicatedPlan</Plan>
              </WebSpaceToCreate>
            </Site>", site.SiteName, site.WebSpaceGeo, site.WebSpaceName);

        req.Method = "POST";        // Post method
        //You can also use ContentType = "text/xml";

        // with the request
        req.UserAgent = "Fiddler";
        req.Headers.Add("x-ms-version", "2013-08-01");
        req.ClientCertificates.Add(cert);
        // Attaching the Certificate To the request

        // when you browse manually you get a dialogue box asking 
        // that whether you want to browse over a secure connection.
        // this line will suppress that message
        //(pragramatically saying ok to that message). 

        string postData = dataToPost;
        var encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(postData);

        // Set the content length of the string being posted.
        req.ContentLength = byte1.Length;

        Stream newStream = req.GetRequestStream();

        newStream.Write(byte1, 0, byte1.Length);

        // Close the Stream object.
        newStream.Close();

        var rsp = (HttpWebResponse)req.GetResponse();

        var reader = new StreamReader(rsp.GetResponseStream());
        String retData = reader.ReadToEnd();

        req.GetRequestStream().Close();
        rsp.GetResponseStream().Close();

        return new HttpResponseMessage
        {
            StatusCode = rsp.StatusCode,
            Content = new StringContent(retData)
        };
    }

1 个答案:

答案 0 :(得分:0)

我不完全确定你在这里尝试实现的目标。但如果我理解正确,你想发布一个程序化的网站。

您无法使用Azure管理API执行此操作(以编程方式发布网站)。 Azure管理API用于管理Azure服务和资源。网站内容本身不是Azure服务,也不是Azure资源。

如果您想以编程方式将网站发布到Azure网站,我建议您仔细阅读How to deploy an Azure Web site

从那里提到的,很容易自动化