asp.net-web-api

Wrapping a Web API response in JSON but still having it work with IQueryable and oData

╄→尐↘猪︶ㄣ 提交于 2021-02-07 04:19:47
问题 I have an ASP.NET Web API project, and I want to use oData filters in the project, with the ASP.NET oData preview package. This means I need to use IQueryable as the response type. Unfortunately, the consumer requires the response wrapped like so: { "total": 2, "success": true, "data": [ { object1 }, { object2 } ] } I created a wrapper object which assigns the IQueryable response from my original version to the "data" property, and sets the values for the "total" and "success" properties as

Wrapping a Web API response in JSON but still having it work with IQueryable and oData

风流意气都作罢 提交于 2021-02-07 04:19:21
问题 I have an ASP.NET Web API project, and I want to use oData filters in the project, with the ASP.NET oData preview package. This means I need to use IQueryable as the response type. Unfortunately, the consumer requires the response wrapped like so: { "total": 2, "success": true, "data": [ { object1 }, { object2 } ] } I created a wrapper object which assigns the IQueryable response from my original version to the "data" property, and sets the values for the "total" and "success" properties as

How do I override OnAuthorization in net core web api?

对着背影说爱祢 提交于 2021-02-07 03:40:06
问题 Earlier I achieved something like this in asp.net public class Authentication : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.Request.Headers.Authorization == null) { actionContext.Response = actionContext.Request .CreateResponse(HttpStatusCode.Unauthorized); } else { var authenticationToken = actionContext.Request.Headers .Authorization.Parameter; var decodedAuthenticationToken = Encoding.UTF8.GetString( Convert

How do I override OnAuthorization in net core web api?

∥☆過路亽.° 提交于 2021-02-07 03:37:59
问题 Earlier I achieved something like this in asp.net public class Authentication : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.Request.Headers.Authorization == null) { actionContext.Response = actionContext.Request .CreateResponse(HttpStatusCode.Unauthorized); } else { var authenticationToken = actionContext.Request.Headers .Authorization.Parameter; var decodedAuthenticationToken = Encoding.UTF8.GetString( Convert

How do I override OnAuthorization in net core web api?

无人久伴 提交于 2021-02-07 03:37:23
问题 Earlier I achieved something like this in asp.net public class Authentication : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.Request.Headers.Authorization == null) { actionContext.Response = actionContext.Request .CreateResponse(HttpStatusCode.Unauthorized); } else { var authenticationToken = actionContext.Request.Headers .Authorization.Parameter; var decodedAuthenticationToken = Encoding.UTF8.GetString( Convert

How do I override OnAuthorization in net core web api?

两盒软妹~` 提交于 2021-02-07 03:36:40
问题 Earlier I achieved something like this in asp.net public class Authentication : AuthorizationFilterAttribute { public override void OnAuthorization(HttpActionContext actionContext) { if (actionContext.Request.Headers.Authorization == null) { actionContext.Response = actionContext.Request .CreateResponse(HttpStatusCode.Unauthorized); } else { var authenticationToken = actionContext.Request.Headers .Authorization.Parameter; var decodedAuthenticationToken = Encoding.UTF8.GetString( Convert

What is the difference between Microsoft.AspNet.WebApi.OData and Microsoft.Data.OData and Microsoft.AspNet.OData?

偶尔善良 提交于 2021-02-06 15:15:40
问题 I am creating a RESTful service using Web API and Entity Framework with OData endpoints. The Microsoft.AspNet.WebApi.OData and Microsoft.Data.OData and Microsoft.AspNet.OData packages seem to overlap, so I wasn't sure which one to use. What are the differences between them? What are the pros and cons of each? 回答1: Microsoft.AspNet.OData is the one you'll most likely want to use for a new project. It sets up Web API to use the OData 4 protocol. Microsoft.AspNet.WebApi.OData is the older

What is the difference between Microsoft.AspNet.WebApi.OData and Microsoft.Data.OData and Microsoft.AspNet.OData?

与世无争的帅哥 提交于 2021-02-06 15:14:24
问题 I am creating a RESTful service using Web API and Entity Framework with OData endpoints. The Microsoft.AspNet.WebApi.OData and Microsoft.Data.OData and Microsoft.AspNet.OData packages seem to overlap, so I wasn't sure which one to use. What are the differences between them? What are the pros and cons of each? 回答1: Microsoft.AspNet.OData is the one you'll most likely want to use for a new project. It sets up Web API to use the OData 4 protocol. Microsoft.AspNet.WebApi.OData is the older

Multiple Calls to HttpContent ReadAsAsync

纵饮孤独 提交于 2021-02-06 14:53:30
问题 Using Web API 2.2, suppose I want to read from HttpContent twice, each time as a different type. await httpContent.LoadIntoBufferAsync(); //necessary to buffer content for multiple reads var X = await httpContent.ReadAsAsync<T>(); //read as first type var Y = await httpContent.ReadAsAsync<Dictionary<string, object>>(); //read as second type When I run the above code, X is a non-null instance of T while Y is null. If I switch the order, Y will be a non-null dictionary while X will be null. In

Ignore async without await compilation warning

爱⌒轻易说出口 提交于 2021-02-06 12:46:27
问题 I have a base controller with the following abstract method: [HttpDelete] public abstract Task<IHttpActionResult> Delete(int id); In one particular controller, I don't want to implement deletion, so the method looks like this: public override async Task<IHttpActionResult> Delete(int id) { return ResponseMessage(Request.CreateResponse(HttpStatusCode.MethodNotAllowed, new NotSupportedException())); } Although the above code compiles, I get a warning: This async method lacks 'await' operators