IUrlHelper.Action为Delete操作方法返回null

问题描述

我正在努力了解IUrlHelper.Action()方法。我创建了一个新的ASP.NET Core 3.1 Web API,并向Get()添加Delete()WeatherForecastController操作方法(您可以忽略实现):

[ApiController]
[ApiVersion("2")]
[Route("api/v{version:apiVersion}/[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)],// see here
            Get = Url.Action(nameof(WeatherForecastController.Get),"WeatherForecast",new { id = 1 },"https"),Delete = Url.Action(nameof(WeatherForecastController.Delete),})
        .ToArray();
    }

    [HttpGet]
    [Route("{id}")]
    public IActionResult Get([Fromroute] int id)
    {
        var rng = new Random();
        return Ok(Enumerable.Range(1,1).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),Summary = Summaries[rng.Next(Summaries.Length)]
        })
        .ToArray());
    }

    [HttpDelete]
    [Route("{id}")]
    public IActionResult Delete([Fromroute] int id)
    {
        return NoContent();
    }
}

如您所见,我还添加了API版本控制。 Startup如下:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        services
            .AddApiVersioning(options =>
            {
                options.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1,0);
                options.AssumeDefaultVersionWhenUnspecified = true;
                options.ApiVersionReader = new UrlSegmentApiVersionReader();
                options.ErrorResponses = new ApiVersioningErrorResponseProvider();
                options.RegisterMiddleware = true;
                options.ReportApiVersions = true;
            })
            .AddVersionedApiExplorer(options =>
            {
                options.GroupNameFormat = "VVV";
                options.SubstituteApiVersionInUrl = true;
            });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

致电api/v2/WeatherForcast时,我得到以下结果:

[
    {
        "date": "2020-10-31T09:30:07.8675547+01:00","temperatureC": -3,"temperatureF": 27,"summary": "Chilly","get": "https://localhost:44302/api/v2/WeatherForecast/1","delete": null
    },{
        "date": "2020-11-01T09:30:07.8989581+01:00","temperatureC": -2,"temperatureF": 29,"summary": "Freezing",{
        "date": "2020-11-02T09:30:07.8990626+01:00","temperatureC": 26,"temperatureF": 78,"summary": "Balmy",{
        "date": "2020-11-03T09:30:07.8990829+01:00","temperatureC": 24,"temperatureF": 75,"summary": "Scorching",{
        "date": "2020-11-04T09:30:07.8991014+01:00","temperatureC": 52,"temperatureF": 125,"delete": null
    }
]

有人可以解释为什么"delete"属性null吗?我看不到任何原因,为什么"delete"不应包含与"get"相同的链接。非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)