asp.net-web-api

Exclude certain fields when returning as json

喜欢而已 提交于 2020-03-23 06:01:25
问题 I have an asp.net web api application. For now lets say that the application consists of a User entity and a Post entity. A post is written by a user, so every post entity contains a reference to the user entity. class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public User User { get; set; } // Reference to the user that wrote the post } The problem is when i want to return a list of posts as Json. I don't want to include the

Exclude certain fields when returning as json

匆匆过客 提交于 2020-03-23 06:00:29
问题 I have an asp.net web api application. For now lets say that the application consists of a User entity and a Post entity. A post is written by a user, so every post entity contains a reference to the user entity. class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public User User { get; set; } // Reference to the user that wrote the post } The problem is when i want to return a list of posts as Json. I don't want to include the

How to set globalization?

蹲街弑〆低调 提交于 2020-03-21 06:58:29
问题 I'm stuck in an ASP.NET Web API Globalization problem. I want to get date time in culture specific format when I invoke a Web API. Please provide some insights. In my solution, first, there is one delegating handler, whose job is to set the culture. It is defined as follows: using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Web; namespace CultureHandling.Handlers { public class

What is the proper way to handle a generic object in a WEBAPI method?

。_饼干妹妹 提交于 2020-03-20 06:56:06
问题 The scenario is something like this: I have this web-api which will handle a variety of payment gateways, and I want to have the same endpoint for all of those. So, what I have in mind is to get some json data like this: { "OperationId":"N0004", "Generic_Object_That_Will_Change_According_ToThe_GateWay": { "Sale_id":1000, "CodUser":"1000040", "Email":"teste@teste.com" } } Or this, for some other payment gateway { "OperationId":"N044444", "Generic_Object_That_Will_Change_According_ToThe_GateWay

ASP.NET Core EventLog provider

做~自己de王妃 提交于 2020-03-18 21:39:43
问题 I have a project using .NET Framework ASP.NET Core 2.0, and want to implement logging to windows event log, like i read here Add log providers public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); logging.AddEventSourceLogger(); logging

JSON string encoded twice?

徘徊边缘 提交于 2020-03-18 10:17:41
问题 My Web API (code that generates JSON) is generating the following JSON string. It seems, if I am not wrong, that it has been encoded twice: "\"[{\\\"SportID\\\":1,\\\"SportName\\\":\"Tennis\\\"},{\"SportID\\\":2,\\\"SportName\\\":\\\"Footbal\\\"},{\"SportID\\\":3,\"SportName\":\\\"Swimming\\\"}]\"" Web API code: public string JSONTest() { List<Sport> sports = new List<Sport>(); sports.Add(new Sport() { SportID = 1, SportName = "Tennis" }); sports.Add(new Sport() { SportID = 2, SportName =

“The requested resource does not support http method 'OPTIONS'” when using EnableCors

淺唱寂寞╮ 提交于 2020-03-18 04:59:09
问题 I want to enable CORS on one specific action in an Asp.net Web Api. Here's how I'm trying to do it: [Route("api/mycontroller/myaction")] [HttpPost] [EnableCors("https://example.com", "*", "post")] public async Task<IHttpActionResult> MyAction() { ... } But when I send an OPTIONS request to the route, I get back an error: "The requested resource does not support http method 'OPTIONS'." I also tried removing the [HttpPost] annotation to no avail. What am I missing? 回答1: You've probably missed

Error posting JSON to Web API 2 : The request entity's media type 'text/plain' is not supported for this resource

夙愿已清 提交于 2020-03-18 03:46:38
问题 I have this class : public class MyClass { public MyClass() { Secret = "Don't tell me"; } public string Name { get; set; } public int Age { get; set; } public string Description { get; set; } private string Secret { get; set; } } And this WEB API method : // POST api/fixture public HttpResponseMessage Post(MyClass value) { return new HttpResponseMessage(HttpStatusCode.Created); } I've set Web API to return JSON instead of XML and I haven't done any other change to the default config. I'm

Referencing system.web.cors

喜夏-厌秋 提交于 2020-03-17 07:55:26
问题 I'm trying to implement cors support in my Web API. I have read a couple of blog posts on this topic, but I can't seem to find System.Web.Cors.dll or System.Web.Http.Cors.dll Is there something I need to install? 回答1: Create a .NET 4.5 MVC project, install nuget package "Microsoft ASP.NET Web API [version] Cross-Origin Support" (search for "cors" and it will be on 1st or 2nd place in list). That's it. P.S. If you encounter errors try to install "Microsoft ASP.NET Web API [version]" package

Asp.Net MVC + WebApi 2 combine paths when calling WebApi from Angular 8 app

我只是一个虾纸丫 提交于 2020-03-17 03:15:59
问题 I have one question with witch I have having issues for some time now. First, a little background. I have created an MVC app that holds Angular 8 app. Nothing special about it. I also have WebApi 2 app that this Angular app is supposed to "talk" with. The problem I have is, when I build everything and call Api (https) from MVC with Angular (http) I get correct URL to Api. You can see picture 1. Picture 1 But, see what happens when I call Api (http) from MVC with Angular (http). Please, see