asp.net-core-mvc

Custom model binder for a webapi method with more than one parameter

夙愿已清 提交于 2020-05-13 07:31:40
问题 WHAT I HAVE I have an api controller (ASP.NET Core MVC) with the following method: [HttpPost] [Route("delete")] public Task<ActionResult> SomeAction(Guid[] ids, UserToken userToken, CancellationToken cancellationToken) { .... } I have a custom model binder and binder provider: public class UserTokenBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context

Custom model binder for a webapi method with more than one parameter

血红的双手。 提交于 2020-05-13 07:30:09
问题 WHAT I HAVE I have an api controller (ASP.NET Core MVC) with the following method: [HttpPost] [Route("delete")] public Task<ActionResult> SomeAction(Guid[] ids, UserToken userToken, CancellationToken cancellationToken) { .... } I have a custom model binder and binder provider: public class UserTokenBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context

Custom model binder for a webapi method with more than one parameter

时间秒杀一切 提交于 2020-05-13 07:29:07
问题 WHAT I HAVE I have an api controller (ASP.NET Core MVC) with the following method: [HttpPost] [Route("delete")] public Task<ActionResult> SomeAction(Guid[] ids, UserToken userToken, CancellationToken cancellationToken) { .... } I have a custom model binder and binder provider: public class UserTokenBinderProvider : IModelBinderProvider { public IModelBinder GetBinder(ModelBinderProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context

ToArrayAsync() throws “The source IQueryable doesn't implement IAsyncEnumerable”

只愿长相守 提交于 2020-05-12 12:12:18
问题 I have a MVC project on ASP.NET Core, my problem is connected with IQueryable and asynchronous. I wrote the following method for search in IQueryable<T> : private IQueryable<InternalOrderInfo> WhereSearchTokens(IQueryable<InternalOrderInfo> query, SearchToken[] searchTokens) { if (searchTokens.Length == 0) { return query; } var results = new List<InternalOrderInfo>(); foreach (var searchToken in searchTokens) { //search logic, intermediate results are being added to `results` using `AddRange(

ToArrayAsync() throws “The source IQueryable doesn't implement IAsyncEnumerable”

不打扰是莪最后的温柔 提交于 2020-05-12 12:11:43
问题 I have a MVC project on ASP.NET Core, my problem is connected with IQueryable and asynchronous. I wrote the following method for search in IQueryable<T> : private IQueryable<InternalOrderInfo> WhereSearchTokens(IQueryable<InternalOrderInfo> query, SearchToken[] searchTokens) { if (searchTokens.Length == 0) { return query; } var results = new List<InternalOrderInfo>(); foreach (var searchToken in searchTokens) { //search logic, intermediate results are being added to `results` using `AddRange(

ToArrayAsync() throws “The source IQueryable doesn't implement IAsyncEnumerable”

十年热恋 提交于 2020-05-12 12:09:32
问题 I have a MVC project on ASP.NET Core, my problem is connected with IQueryable and asynchronous. I wrote the following method for search in IQueryable<T> : private IQueryable<InternalOrderInfo> WhereSearchTokens(IQueryable<InternalOrderInfo> query, SearchToken[] searchTokens) { if (searchTokens.Length == 0) { return query; } var results = new List<InternalOrderInfo>(); foreach (var searchToken in searchTokens) { //search logic, intermediate results are being added to `results` using `AddRange(

How to unit test a Controller action using the Response property in ASP.NET 5 (MVC 6)? [duplicate]

℡╲_俬逩灬. 提交于 2020-05-12 11:32:09
问题 This question already has answers here : MOQ - Mocking MVC Controller's Response.Cookies.Clear() (2 answers) Closed 4 years ago . In an ASP.NET Core 1.0 (MVC 6) project I have a Controller action method in which I use the Response property to set a header: [HttpGet] public IActionResult Get() { ... Response.Headers.Add("Location", location); ... } I tried to implement a unit test for this action method, but the value of the Response property is null. This was easy to solve in the previous

async provider in .net core DI

亡梦爱人 提交于 2020-05-09 20:52:19
问题 I'm just wondering if it's possible to have async/await during DI. Doing the following, the DI fails to resolve my service. services.AddScoped(async provider => { var client = new MyClient(); await client.ConnectAsync(); return client; }); where as the following works perfectly fine. services.AddScoped(provider => { var client = new MyClient(); client.ConnectAsync().Wait(); return client; }); 回答1: Async/await doesn't make sense when resolving dependencies, because: Constructors can't be

How to include Query String in the route resolution in order to allow multiple Actions with the same Method, Route and Query String?

☆樱花仙子☆ 提交于 2020-05-09 07:54:28
问题 I am converting an ASP.NET MVC (.NET Framework) application to ASP.NET Core MVC. This is strictly a conversion, I cannot make any breaking changes hence I cannot change any Routes or Methods. I am unable to match the same functionality in ASP.NET Core MVC. Working ASP.NET MVC: [HttpPut] [Route("status")] public async Task<IHttpActionResult> UpdateStatusByOrderGuid([FromUri] Guid orderGUID, [FromBody] POST_Status linkStatusModel) { } [HttpPut] [Route("status")] public async Task

How to unit test whether a Core MVC controller action calls ControllerBase.Problem()

こ雲淡風輕ζ 提交于 2020-05-09 07:39:26
问题 We have a controller that derives from ControllerBase with an action like this: public async Task<ActionResult> Get(int id) { try { // Logic return Ok(someReturnValue); } catch { return Problem(); } } We also have a unit test like this: [TestMethod] public async Task GetCallsProblemOnInvalidId() { var result = sut.Get(someInvalidId); } But ControllerBase.Problem() throws a Null Reference Exception. This is a method from the Core MVC framework, so I don't realy know why it is throwing the