안녕하세요.
Visual Studio에서 Blazor Server앱 기본 생성하면 만들어지는 템플릿에서
Components/Pages/Home.razor 부분과 Program.cs만 수정해 코드를 작성했습니다.
Azure translator api를 사용하기 위해서
~Home.razor파일에
<!--장문의 텍스트 입력받기-->
<textarea @bind="LongText" class="form-control"></textarea>
<button class="btn-primary" @onclick="Translate">Translate</button>
<p role="textbox">번역 결과 : @NewText</p>
@code
{
private string Message = "Hello";
private string LongText = "";
private string NewText = "";
private static readonly string key = "~~~~";
private static readonly string endpoint = "~~~~";
private static readonly string location = "eastus";
private async Task Translate()
{
string route = "/translate?api-version=3.0&from=ko&to=en";
object[] body = new object[] { new { Text = LongText } };
var requestBody = JsonSerializer.Serialize(body);
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
request.Headers.Add("Ocp-Apim-Subscription-Region", location);
HttpResponseMessage response = await Http.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
var translation = JsonSerializer.Deserialize<List<TranslationResult>>(result);
NewText = translation[0].Translations[0].Text;
}
else
{
NewText = $"Translation failed: {response.StatusCode}";
}
StateHasChanged();
}
}
public class TranslationResult
{
public List<Translation> Translations { get; set; } = new List<Translation>();
}
public class Translation
{
public string Text { get; set; } = "";
public string To { get; set; } = "";
}
}
이렇게 코드를 작성했는데 동작이 안 되네요ㅠ
개발자 도구에
blazor.web.js:1 [2024-05-20T09:33:44.467Z] Error: There was an unhandled exception on the current circuit, so this circuit will be terminated. For more details turn on detailed exceptions by setting ‘DetailedErrors: true’ in ‘appSettings.Development.json’ or set ‘CircuitOptions.DetailedErrors’.
이렇게 뜨는데
이게 어딜 고쳐야 하는 건지 감이 전혀 안 잡히네요
이렇게만 떠서..어떻게 해야할지 모르겠습니다 ㅠㅠㅠ 도움 주신다면 너무 감사할 것 같습니다.