【问题标题】:Adding a custom api endpoint to the ASP:Net Core template向 ASP:Net Core 模板添加自定义 api 端点
【发布时间】:2020-03-03 22:25:51
【问题描述】:

我尝试为 http 请求添加自己的端点。 我从作为 asp.net 核心角度模板一部分的天气预报中复制了逻辑并对其进行了修改:

原控制器代码:

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;

    namespace AngularDemo.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class WeatherForecastController : ControllerBase
        {
            private static readonly string[] Summaries = new[]
            {
                "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
            };

            private readonly ILogger<WeatherForecastController> _logger;

            public WeatherForecastController(ILogger<WeatherForecastController> logger)
            {
                _logger = logger;
            }

            [HttpGet]
            public IEnumerable<WeatherForecast> Get()
            {
                var rng = new Random();
                return Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = rng.Next(-20, 55),
                    Summary = Summaries[rng.Next(Summaries.Length)]
                })
                .ToArray();
            }
        }
    }

这是我的测试控制器

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;

    namespace AngularDemo.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class TestController: ControllerBase
        {

            private readonly ILogger<WeatherForecastController> _logger;

            public TestController(ILogger<TestController> logger)
            {
                _logger = logger;
            }

            [HttpGet]
            public IEnumerable<string> Get()
            {

                return Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = 100,
                    Summary = "Test"
                })
                .ToArray();
            }
        }
    }

但我现在的问题是,如何从我的 Angular 应用程序中调用它? 这是我的 Angular 组件,其中有“天气预报”作为参数。我应该在另一个组件中哪里配置它,它应该向 TestController 发出请求?

角度天气组件:

import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-fetch-data',
  templateUrl: './fetch-data.component.html'
})
export class FetchDataComponent {
  public forecasts: WeatherForecast[];

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<WeatherForecast[]>(baseUrl + 'weatherforecast').subscribe(result => {
      this.forecasts = result;
    }, error => console.error(error));
  }
}

interface WeatherForecast {
  date: string;
  temperatureC: number;
  temperatureF: number;
  summary: string;
}

【问题讨论】:

    标签: angular asp.net-core asp.net-core-webapi


    【解决方案1】:

    您的控制器用[Route("[controller]")] 装饰。这意味着路由将设置为等于您的控制器类的名称减去后缀“Controller”。

    在您的情况下:TestController 等同于 [Route("test")]

    然后您可以更改控制器类的名称或硬编码[Route] 属性的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多