| | |
| | | using System.ComponentModel.DataAnnotations; |
| | | using WIDESEAWCS_S7Simulator.Core.Entities; |
| | | using WIDESEAWCS_S7Simulator.Core.Enums; |
| | | using WIDESEAWCS_S7Simulator.Web.Services; |
| | | |
| | | namespace WIDESEAWCS_S7Simulator.Web.Pages; |
| | | |
| | | /// <summary> |
| | | /// 创建实例页 |
| | | /// </summary> |
| | | public class CreateModel : PageModel |
| | | public class CreateModel : BasePageModel |
| | | { |
| | | private readonly ILogger<CreateModel> _logger; |
| | | private readonly IHttpClientFactory _httpClientFactory; |
| | | private readonly ApiHttpClient _apiClient; |
| | | |
| | | public CreateModel(ILogger<CreateModel> logger, IHttpClientFactory httpClientFactory) |
| | | public CreateModel(ILogger<CreateModel> logger, IConfiguration configuration, ApiHttpClient apiClient) |
| | | : base(configuration) |
| | | { |
| | | _logger = logger; |
| | | _httpClientFactory = httpClientFactory; |
| | | _apiClient = apiClient; |
| | | } |
| | | |
| | | [BindProperty] |
| | |
| | | |
| | | try |
| | | { |
| | | // 创建HttpClient并设置BaseAddress |
| | | var httpClient = _httpClientFactory.CreateClient(); |
| | | httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}"); |
| | | |
| | | var config = new InstanceConfig |
| | | { |
| | | Id = Input.Id, |
| | |
| | | } |
| | | }; |
| | | |
| | | var response = await httpClient.PostAsJsonAsync("/api/SimulatorInstances", config); |
| | | var result = await _apiClient.CreateInstanceAsync(config); |
| | | |
| | | if (response.IsSuccessStatusCode) |
| | | if (result != null) |
| | | { |
| | | _logger.LogInformation("Instance {InstanceId} created successfully", Input.Id); |
| | | TempData["SuccessMessage"] = $"实例 \"{Input.Id}\" 创建成功!"; |
| | | return RedirectToPage("/Index"); |
| | | } |
| | | else if (response.StatusCode == System.Net.HttpStatusCode.Conflict) |
| | | { |
| | | ModelState.AddModelError(string.Empty, "实例ID已存在,请使用其他ID"); |
| | | } |
| | | else |
| | | { |
| | | var error = await response.Content.ReadFromJsonAsync<object>(); |
| | | ModelState.AddModelError(string.Empty, error?.ToString() ?? "创建实例失败"); |
| | | ModelState.AddModelError(string.Empty, "创建实例失败"); |
| | | } |
| | | } |
| | | catch (HttpRequestException ex) |