gpt4 book ai didi

Kindly suggest how to pass null parameter in between of route(请建议如何在路由之间传递null参数)

转载 作者:bug小助手 更新时间:2023-10-22 16:38:53 27 4
gpt4 key购买 nike



I am new in dotnet. Kindly suggest how to pass null parameter within the route.
My code is working for below routes:
https://localhost:7242/WeatherForecast/1/1/1
https://localhost:7242/WeatherForecast/1/1/
https://localhost:7242/WeatherForecast/1/

我是网络新手。请建议如何在路由中传递null参数。我的代码适用于以下路线:https://localhost:7242/WeatherForecast/1/1/1https://localhost:7242/WeatherForecast/1/1/https://localhost:7242/WeatherForecast/1/


but not working for below URL, I am getting 404 error.
https://localhost:7242/WeatherForecast/1//1

但不适用于以下URL,我得到404错误。https://localhost:7242/WeatherForecast/1//1


It was working fine in .net4.5 but not working in 6.0

它在.net4.5中运行良好,但在6.0中不起作用


[HttpGet("{id}/{p1?}/{p2?}")]
public IEnumerable<WeatherForecast> Get(int id, int? p1 = 0, int? p2 = 0){
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]}).ToArray();
}

更多回答

You cannot have optional parameters in the middle of route. The route engine could only map the last optional parameter. So you could only use the querystring instead of the friendly url could solve the issue.

不能在管线中间有可选参数。路由引擎只能映射最后一个可选参数。所以你只能使用querystring而不是友好的url来解决这个问题。

优秀答案推荐

Modify your route to accept these parameters as query strings.

修改路由以接受这些参数作为查询字符串。


In ASP.NET 6.0, there was a change in how route parameters are handled. The default behaviour of route parameters in ASP.NET 6.0 does not allow empty or null values.

在ASP.NET 6.0中,路由参数的处理方式发生了变化。ASP.NET 6.0中路由参数的默认行为不允许为空或null值。


[HttpGet("{id}")]
public IEnumerable<WeatherForecast> Get(int id, int? p1 = null, int? p2 = null){
// Your code here
}

This will allow routes like these to work

这将允许这样的路线工作


https://localhost:7242/WeatherForecast/1?p1=1&p2=
https://localhost:7242/WeatherForecast/1?p1=&p2=1
https://localhost:7242/WeatherForecast/1?p1=&p2=

更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com