wanshenmean
2026-03-13 d216edd0e9931d71664f33e625cff6d8131a0fad
Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Web/Pages/Create.cshtml.cs
@@ -3,22 +3,23 @@
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 HttpClient _httpClient;
    private readonly ApiHttpClient _apiClient;
    public CreateModel(ILogger<CreateModel> logger, IHttpClientFactory httpClientFactory)
    public CreateModel(ILogger<CreateModel> logger, IConfiguration configuration, ApiHttpClient apiClient)
        : base(configuration)
    {
        _logger = logger;
        _httpClient = httpClientFactory.CreateClient();
        _httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
        _apiClient = apiClient;
    }
    [BindProperty]
@@ -58,22 +59,17 @@
                }
            };
            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)