我有一个名为WeatherForecast的 WebAPI 控制器,只有一个操作。操作方法如下:
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
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();
}
Run Code Online (Sandbox Code Playgroud)
但是,HttpGet.Name = "GetWeatherForecast"应该定义一个路线名称“GetWeatherForecast”,因为我了解Name此属性的属性的用途。
但是 Swagger 向我显示该操作本身根本没有路由:显示的 URL 是https://localhost:port/WeatherForecast (可以通过该 URL 消费服务操作,我使用 Postman 进行测试)
但是对于设置了属性HttpGet的属性Name,我希望它是 https://localhost:port/WeatherhForecast/ GetWeatherForecast
当我在操作方法上另外使用Route属性 ( Route("GetWeatherForecast")) 时,操作的路由如下所示: https://localhost:port/WeaterhForecast/ GetWeatherForecast
(现在确实可以通过该 URL 访问服务操作)。
那么,问题是:为什么attributeName的属性HttpGet没有实现文档所承诺的功能?或者说真正的目的是什么HttpGetAttribute.Name?
源代码是使用 .NET 6.0 和 …
我有一个带有 OAuth2 (Microsoft Identity) 的 WebAPI,我需要支持“客户端凭据流”(对于非交互式驱动的客户端,具有角色)以及“身份验证凭据流”(对于人工交互客户端,具有范围)。
如何使其与“MyRole”或“MyScope”一起运行?...当控制器基本上看起来像这样:
[Authorize(Roles = "MyRole")]
[RequiredScope("MyScope")]
public class MyController : ControllerBase
{
}
Run Code Online (Sandbox Code Playgroud)
我使用 .NET 6
我的目标是将 XAML 中的元素属性绑定到类背后代码的属性,而 DataContext 仍然是 ViewModel。
原因是,我在 XAML 中有一些唯一的 UI 装饰属性,这些属性不是由 ViewModel 控制而是由后面的代码控制。
所以基本上我搜索类似的东西:
<Element
Attribute = "{Binding ThatOneCodeBehind.WhateverProperty}"
OtherAttribute1 = "{Binding StillDataContextSomething}"
OtherAttribute2 = "{Binding StillDataContextSomething}"
/>
Run Code Online (Sandbox Code Playgroud)
正确的绑定语法是什么Attribute="{Binding ThatOneCodeBehind:WhateverProperty}"?