首页 > 解决方案 > 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'");
        }
    }
}

我应该在哪里粘贴代码以及如何链接它以在单击按钮的情况下运行?

在此先感谢,尼古拉

标签: c#asp.netasp.net-mvcasp.net-mvc-4asp.net-mvc-5

解决方案


如果你想从客户端的按钮执行你的方法,你可以试试这个。

在 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'");
        //}



    }

推荐阅读