How to create Azure App Service from Web API?

Viewed 345

I am creating a .Net Core(v3.1) web application that I have to create a new .Net Core(v3.1) app service from the Azure Management API. I am able to check availability but when I try to find that I am not able to find that but I am able to create App Service Plan but not able to create an app service.

Link to Create App Service Plan: Here

Link for Rest API Documentation: Here

Code to Generate Access Token

var client = new RestClient("https://login.microsoftonline.com/<SubscriptionID>/oauth2/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", "<ClientID>");
request.AddParameter("client_secret", "<ClientSecret>");
request.AddParameter("resource", "https://management.azure.com/");
IRestResponse Iresponse = client.Execute(request);
Token token = JsonConvert.DeserializeObject<Token>(Iresponse.Content);

Code To Check Availablity

using (var Httpclient = new HttpClient())
{
    Httpclient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.access_token);
    var baseUrl = new Uri($"https://management.azure.com/");
    var requestURl = baseUrl + @"/subscriptions/<SubscriptionID>/providers/Microsoft.Web/checknameavailability?api-version=2019-08-01";
    string body = "{\"name\":\"AvaiableCheck\",\"type\":\"slot\"}";
    var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
    var response = Httpclient.PostAsync(requestURl, stringContent).Result;
    var a = await response.Content.ReadAsStringAsync();
}

If I get available in response I have to create a new app service in Azure App Service.

I am calling the below API of the PUT call.

var requestURl = baseUrl + @"/subscriptions/<SubscriptionGUID>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Web/sites/<SiteName>?api-version=2021-04-01";

In Response I get the below error:

{
  "error": {
    "code": "MismatchingResourceName",
    "message": "The provided resource name 'TestAPIFromResource' did not match the name in the Url '<ResourceName>'."
  }
}

If anyone has an idea about that please let me know it helps me a lot.

0 Answers
Related