简单MVC WEB API -无法隐式转换类型System.Threading. Tasks< microsoft.aspnetcore.mvc.IAcation>

fjaof16o  于 2023-01-18  发布在  .NET
关注(0)|答案(1)|浏览(296)

MyWeatherData = WeatherAPI.GetMyWeather();行出错
错误=无法隐式转换类型system.threading.tasks.task<microsoft.aspnetcore.mvc.IAcation>
问题详细信息:我从来没有使用过MVC之前,我对MVC的流程感到困惑。目前,从view,我调用HomeController。而不是从HomeController,我调用2nd WeatherController。我知道这是错误的,但现在确定如何做到这一点,而不创建这么多的控制器
**我想做的是:**我有一个视图(索引),其中有一个按钮。如果按钮被点击,我想使用天气API来获取数据,保存在模型类中,并在视图(索引)中显示数据。

Index.cshtml文件-这个视图有一个按钮。如果你点击它,它会发送一个post请求到HomeController

<form asp-controller="Home" method="post">
        <button type="submit" >Submit</button>
</form>
// Display weather data here on click

家庭控制器类

public WeatherModel MyWeatherData { get; set; } = default!;
    public WeatherController WeatherAPI { get; set;  }

    public IActionResult Index()
    {
        MyWeatherData = WeatherAPI.GetMyWeather();
        return View();
    }

天气控制器类

[Route("api/[controller]")]
[ApiController]
public class WeatherController : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> GetMyWeather()
    {
        var latitude = 40.712776;
        var longitude = -74.005974;

        using (var client = new HttpClient())
        {
            try
            {
                client.BaseAddress = new Uri("https://api.open-meteo.com");
                var response = await client.GetAsync($"/v1/forecast?latitude={latitude}&longitude={longitude}&hourly=temperature_2m");
                response.EnsureSuccessStatusCode();

                var stringResult = await response.Content.ReadAsStringAsync(); //get json data in string 
                var rawWeather = JsonConvert.DeserializeObject<WeatherModel>(stringResult);

                WeatherModel WM = new WeatherModel();
                WM.latitude = rawWeather.latitude;
                WM.longitude = rawWeather.longitude;
                WM.generationtime_ms = rawWeather.generationtime_ms;
                WM.utc_offset_seconds = rawWeather.utc_offset_seconds;
                WM.timezone = rawWeather.timezone;
                WM.timezone_abbreviation = rawWeather.timezone_abbreviation;
                WM.elevation = rawWeather.elevation;
                
                return Ok(WM);
            }
            catch (HttpRequestException httpRequestException)
            {
                return BadRequest($"Error getting weather from OpenWeather: {httpRequestException.Message}");
            }
        }
    }//end of method

型号等级

public class WeatherModel
{
    public long latitude { get; set; }
    public long longitude { get; set; }
    public long generationtime_ms { get; set; }
    public long utc_offset_seconds { get; set; }

    public string timezone { get; set; }
    public string timezone_abbreviation { get; set; }
    public string elevation { get; set; }
}
zaqlnxep

zaqlnxep1#

您在这里问了几个不同的问题,但看起来最终您想知道如何解决编译问题。
正如您在WeatherController操作中所看到的,它是异步的,由async关键字和Task类型指示。
public async Task<IActionResult> GetMyWeather()
Task<IActionResult>表示您必须等待响应才能获得IActionResult类型的响应。为此,您还应将HomeController操作更改为异步:

public async Task<IActionResult> Index()
{
    MyWeatherData = await WeatherAPI.GetMyWeather();
    return View();
}

相关问题