asp.net-web-api2

Web Api with OData v4 throwing exception on $select

让人想犯罪 __ 提交于 2020-01-13 03:23:26
问题 I'm using the latest version of WebApi and OData and everything is set up to work right. The only problem is when I try to use $select . It throws the error bellow Object of type 'System.Linq.EnumerableQuery`1[System.Web.OData.Query.Expressions.SelectExpandBinder+SelectAll`1[WebApplication1.Controllers.Person]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[WebApplication1.Controllers.Person]'. I looked at the documentation and their suggestion is to use [Queryable] on

AngularJS Allow-Origin to WebApi2

我们两清 提交于 2020-01-11 12:00:22
问题 I want to host my API on a separate domain. I have configured my auth-interceptor for token in angular with a bearer: config.headers.Authorization = 'Bearer ' + sessionStorage.getItem('token'); In my My WebApi2 I have configured the WebApiConfig with cors. var cors = new EnableCorsAttribute("http://mydomain.com", "*", "*"); config.EnableCors(cors); And in web.config of the API I also included: <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> I still can´t

Web API 2: OData 4: Actions returning 404

試著忘記壹切 提交于 2020-01-11 09:35:15
问题 I have an OData v4 action method which is not working; note however that it was working fine in OData v3 (I am obviously in the process of trying to update my project) OData Action Method: [HttpPost] public Translation Translate(ODataActionParameters parameters) { // Implementation } Configuration: ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<Page>("Pages"); //etc (Other Entity Sets) var pageEntityType = builder.EntityType<Page>(); var translateAction =

Memory Cache in web api

别说谁变了你拦得住时间么 提交于 2020-01-10 14:46:43
问题 I was looking for Caching in my web api where i can use output of one api method(that changes once in 12hrs) for subsequesnt calls and then i found this solution on SO,but i am having a difficulty in understanding and using the below code private IEnumerable<TEntity> GetFromCache<TEntity>(string key, Func<IEnumerable<TEntity>> valueFactory) where TEntity : class { ObjectCache cache = MemoryCache.Default; var newValue = new Lazy<IEnumerable<TEntity>>(valueFactory); CacheItemPolicy policy = new

Memory Cache in web api

霸气de小男生 提交于 2020-01-10 14:45:07
问题 I was looking for Caching in my web api where i can use output of one api method(that changes once in 12hrs) for subsequesnt calls and then i found this solution on SO,but i am having a difficulty in understanding and using the below code private IEnumerable<TEntity> GetFromCache<TEntity>(string key, Func<IEnumerable<TEntity>> valueFactory) where TEntity : class { ObjectCache cache = MemoryCache.Default; var newValue = new Lazy<IEnumerable<TEntity>>(valueFactory); CacheItemPolicy policy = new

How to change default Web API 2 to JSON formatter?

回眸只為那壹抹淺笑 提交于 2020-01-09 12:51:29
问题 I have a Web API project that returns some product data. It negotiates the return type correctly depending on the Accept header (JSON/XML) of the request. The problem is, if no Accept header is specified it returns XML, but I want it to return JSON by default http://website.com/MyPage?type=json // returns json http://website.com/MyPage?type=xml // returns xml http://website.com/MyPage // returns xml by default Here is my current code looks like: GlobalConfiguration.Configuration.Formatters

How to trim in JSON the miliseconds in DateTime serialization

痴心易碎 提交于 2020-01-07 07:43:35
问题 I have a WEB API 2 call, with Entity Framework. If I update a DateTime column from my entity that I read from the database with DateTime.Now , and I serialize it to the client, the column with DateTime that I received from the database has 3 numbers for the milliseconds, but the column with DateTime that I updated in the C# code has 6 digits. Below is the C# code in my controller: [Route("{id:long}/updatedatetime", Name = "UpdateDateTimeByID")] [HttpPost] [ResponseType(typeof(ClGroup))]

RESTful and Repository returning values from another type architecture

跟風遠走 提交于 2020-01-07 03:10:29
问题 In short, I have two database tables: Languages and Frameworks. The important thing is that there is one-to-many relation between the tables(one language has many frameworks). I am designing a RESTful(WebAPI2) service to consume the information from these tables. I am using the repository pattern. Using EF, I've made a navigational property for the language to reach its frameworks. However, how should that be implemented in the repositories. Is it correct to return a framework collection in

Swagger/Swashbuckle document POST with JSON

↘锁芯ラ 提交于 2020-01-07 03:08:10
问题 I have a challenge with Swashbuckle or Swagger. I'm not sure if the problem is with Swagger or Swashbuckle. I have web api call, implemented both with the parameter as URI params, and with reading the content from the body of the request. One is implemented like this: [Route("application/uri")] [HttpPost] public async Task<string> Post([ModelBinder] Application application) { return await RegisterApplication(application); } The other is implemented like this: [Route("loanapplication/json")]

WebAPI 2 Odata Filter not working

余生长醉 提交于 2020-01-07 02:32:07
问题 I have a problem with using multiple filters on my WebAPI 2 Odata project. We only want JSON output and only have one object type to query so we set the url to "/" without the possibility to use a different controller. I have an object I want to query with the following properties: public class Content { public int Id { get; set; } public string Title { get; set; } public string Excerpt { get; set; } public string Link { get; set; } public IList<Tag> Tags { get; set; } } public class Tag {