asp.net-web-api2

Add filter on property for odata query

老子叫甜甜 提交于 2019-12-12 03:28:08
问题 I have an entity: ItContract and every ItContract belongs to an organisation unit. A user logs-in to an organisation unit and have read access to all data. How can I set a filter on organisationUnitId on the server for every odata query? I am using odata v4 with asp.net. 回答1: There is a way to override the queryOption you get in server side. public IHttpActionResult Get(ODataQueryOptions<People> queryOptions) { // get the original request before the alterations HttpRequestMessage

Complex input to WebApi2 function

青春壹個敷衍的年華 提交于 2019-12-12 03:19:48
问题 I've got this class in my Model: public class GetDocParams { public string LogonTicket { get; set; } public int CliRid { get; set; } public string[] ValPairs { get; set; } public string SortBy { get; set; } public int StartRec { get; set; } public int EndRec { get; set; } } This is going to be used as input to a WebApi2 function to retrieve query results from Entity Framework. The function takes the valPairs from input and uses it to build a query that sorts by the passed pairs, i.e. CLI_RID

Injecting a nullable dependency via constructor requires unity to register that nullable dependency

我的未来我决定 提交于 2019-12-12 02:17:09
问题 Just wondering if anyone had this scenario with Unity(as an IoC container) where the class has two injected dependencies(interfaces) where one dependency can be null. For example: public MyServiceDll(IRepository repository, ICanBeNullDependency canBeNullDependency = null) { _repository = repository; _canBeNullDependency = canBeNullDependency; } ICanBeNullDependency is from another assembly. MyServiceDll is another assembly. MyServiceDll is referenced by web api and injected its interface in

How to implement basic authentication in web API 2 (ASP.Net Core)?

霸气de小男生 提交于 2019-12-12 02:14:35
问题 I am new to asp.net core and web API 2 and trying to implement basic authentication in web API like request headers contains the username and password and i will verify them. I have implemented the same thing in Web API using action filters. Now my question is that is it possible to implement basic authentication in web API 2? If possible then can I achieve it with action filters and how? What is the best way to authentication in Web API 2? Should i use middle ware? 回答1: I'm not sure why you

AngularJS - Authentication with Bearer Token and Web API 2.0

烈酒焚心 提交于 2019-12-12 02:02:21
问题 I have the problem that even though I set the $http.defaults.headers.common.Authorization to null I am still capable of accessing the [Authorize] part of my Web API 2.0 application. This problem doesn't arise when I start the application from scratch and try to retrieve the data via an initial GET request. This is when I get an error from the $http callback function. Any guesses why this is happening? I am quite confident that the bearer token is stored somewhere in the browser and doesn't

WebAPI : Min/max double for RouteAttribute

*爱你&永不变心* 提交于 2019-12-12 01:37:41
问题 I have the following API method: [Route("GetEditorialRequestsByCoordinates/{lat:double}/{lng:double}")] [AutomapperExceptionApiFilterAttribute] public HttpResponseMessage GetEditorialRequestsByCoordinates(double lat, double lng) { } it works fine for request like: GET /v1/api/request/GetEditorialRequestsByCoordinates/48.999/2.777/ But I want to add limit (minimum and maximum) for lat and lng. Follow this article: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute

web api Bad Request when getting access token after moving to production

落爺英雄遲暮 提交于 2019-12-12 01:28:29
问题 I have a web api that is working great in test using an access token / bearer authentication. I authenticate and make requests using HttpClient. Easy. Here is the basic web client setup. The base address is a constant that I change when moving to production. public static HttpClient GetClient() { HttpClient Client = new HttpClient(); Client.BaseAddress = new Uri(Ics2Constants.ICS2APIBaseAddress); Client.DefaultRequestHeaders.Accept.Clear(); Client.DefaultRequestHeaders.Accept.Add(new

Adding a model with join to another model for many to many without navigation property [duplicate]

喜你入骨 提交于 2019-12-12 01:16:15
问题 This question already has an answer here : Entity Framework - Inserting model with many to many mapping (1 answer) Closed 12 months ago . How can I insert a model Tag that belongs to a model Post when I have the models setup like this: Post public class Post { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Tag> Tags { get; set; } public Post() { Tags = new List<Tag>(); } } Tag public class Tag { public int Id {

Receiving JSON-formatted data in a Web API 2 action?

陌路散爱 提交于 2019-12-11 23:37:35
问题 I'm working on a RESTful API using JSON for the input and output - requests coming in should have JSON formatted bodies containing relevant data and the response will similarly be JSON formatted. The problem I'm having is that I can't for the life of me figure out how to collect the information from the POST request and have it properly mapped to the relevant objects. For example, I want to allow POST requests for "/contacts" which will contain a JSON formatted Contact object similar to this:

Autofac Injection of data into OWIN Startup class

非 Y 不嫁゛ 提交于 2019-12-11 23:25:59
问题 I've been looking around on SO for a while and can't find the answer to my particular question: How do you use Autofac to inject data (via constructor or property) into the OWIN Startup class? I'm using Autofac in a Windows service which also handles creation of back-end services etc. so I'm doing all the configuration reading and container building in there. I want to be able to register the Startup class so I can inject allowed origins for CORS but even when I register the object with a