c# - ASP.NET MVC 引导项目 - 如何通过单击按钮运行 .cs 文件?
问题描述
所以我用 Bootstrap 做了一个新的 ASP.NET MVC 项目。我想通过单击 html 按钮运行以下代码:(谷歌云文本到语音的代码)
using System;
using System.IO;
using Google.Cloud.TextToSpeech.V1;
public class QuickStart
{
public static void Main(string[] args)
{
// Instantiate a client
TextToSpeechClient client = TextToSpeechClient.Create();
// Set the text input to be synthesized.
SynthesisInput input = new SynthesisInput
{
Text = "Hello, World!"
};
// Build the voice request, select the language code ("en-US"),
// and the SSML voice gender ("neutral").
VoiceSelectionParams voice = new VoiceSelectionParams
{
LanguageCode = "en-US",
SsmlGender = SsmlVoiceGender.Neutral
};
// Select the type of audio file you want returned.
AudioConfig config = new AudioConfig
{
AudioEncoding = AudioEncoding.Mp3
};
// Perform the Text-to-Speech request, passing the text input
// with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = input,
Voice = voice,
AudioConfig = config
});
// Write the binary AudioContent of the response to an MP3 file.
using (Stream output = File.Create("sample.mp3"))
{
response.AudioContent.WriteTo(output);
Console.WriteLine($"Audio content written to file 'sample.mp3'");
}
}
}
我应该在哪里粘贴代码以及如何链接它以在单击按钮的情况下运行?
在此先感谢,尼古拉
解决方案
如果你想从客户端的按钮执行你的方法,你可以试试这个。
在 cshtml 视图中使用:
<div>
<a class="btn btn-success" asp-area="" asp-controller="Home" asp-action="SomeView" >Execute Method</a>
然后在关联的控制器中(我假设它是“HomeController”)使用视图来触发你的方法并返回你想要的任何视图。我不确定这是否正是您想要的希望这会有所帮助
public IActionResult SomeView()
{
TextToSpeech();
return View();
}
public void TextToSpeech()
{
// Instantiate a client
TextToSpeechClient client = TextToSpeechClient.Create();
// Set the text input to be synthesized.
SynthesisInput input = new SynthesisInput
{
Text = "Hello, World!"
};
// Build the voice request, select the language code ("en-US"),
// and the SSML voice gender ("neutral").
VoiceSelectionParams voice = new VoiceSelectionParams
{
LanguageCode = "en-US",
SsmlGender = SsmlVoiceGender.Neutral
};
// Select the type of audio file you want returned.
AudioConfig config = new AudioConfig
{
AudioEncoding = AudioEncoding.Mp3
};
// Perform the Text-to-Speech request, passing the text input
// with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = input,
Voice = voice,
AudioConfig = config
});
//// Write the binary AudioContent of the response to an MP3 file.
//using (System.IO.Stream output = File.Create("sample.mp3"))
//{
// response.AudioContent.WriteTo(output);
// Console.WriteLine($"Audio content written to file 'sample.mp3'");
//}
}
推荐阅读
- c++ - 为什么 std::endl 让这个循环永远运行?
- node.js - 错误:传入的 JSON 对象在 JWT.fromJSON 中不包含 client_email 字段
- python - 对 Y 轴值进行排序
- javascript - 如何在上传时获取云小部件图像信息?
- java - 如何等到所有一系列嵌套的 CompletableFuture 都完成?
- ios - RequestLocation 耗时过长
- java - 在java中返回整数数组时获取java.lang.NullPointerException
- c# - 在视图中显示模型状态错误?
- node.js - Express 中从 http POST 到 http GET 的路由
- javascript - 按名称查找频道然后发送消息