我已經(jīng)創(chuàng)建了一個 .NET Core API 控制器,并且在 POST 方法中,我嘗試使用CreatedAtActionResult方法將路由作為 Location Header 包含在我的響應(yīng)中。[ApiController, Route("v1/[controller]")]public class WidgetController: ControllerBase{ public WidgetController(IWidgetService service) { _service = service; } private readonly IWidgetService _service; [HttpGet("{id}", Name = "GetSingle")] public IActionResult GetSingle(Guid id) { var result = _service.Get(id); return Ok(result); } [HttpPost] public IActionResult Post(WidgetModel model) { var result = _service.Post(model); return result == Guid.Empty ? (IActionResult) BadRequest("No changes saved.") : CreatedAtAction(nameof(GetSingle), new {id = result}, model); }}當(dāng)我啟動應(yīng)用程序時,POSTman 對 POST 的第一次調(diào)用運(yùn)行沒有問題。對象和 Location Header URL 都按預(yù)期生成。但是,如果我在代碼仍在運(yùn)行時再次嘗試訪問該端點(diǎn),則會出現(xiàn)以下錯誤:System.InvalidOperationException:1[Microsoft.AspNetCore.Routing.RouteValuesAddress]' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Routing.DefaultLinkGenerator.GetEndpoints[TAddress](TAddress address)
at Microsoft.AspNetCore.Routing.DefaultLinkGenerator.GetPathByAddress[TAddress](HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues, Nullable在 Microsoft.AspNetCore.Routing.LinkGeneratorRouteValuesAddressExtensions.GetPathByRouteValues
1 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個贊
嘗試設(shè)置CompatibilityVersion.Version_2_0
或2.1
代替2.2
?
EndpointRouting 在 2.2 的掩護(hù)下是一個相當(dāng)大的變化,您的第 3 方附加組件可能不兼容。
將兼容性設(shè)置回 2.1 通常不需要更改控制器中的任何代碼,因此成本相當(dāng)?shù)汀?/p>
- 1 回答
- 0 關(guān)注
- 118 瀏覽
添加回答
舉報
0/150
提交
取消