asp.net-core-1.0

.NET Core: Remove null fields from API JSON response

戏子无情 提交于 2019-12-03 06:29:04
问题 On a global level in .NET Core 1.0 (all API responses), how can I configure Startup.cs so that null fields are removed/ignored in JSON responses? Using Newtonsoft.Json, you can apply the following attribute to a property, but I'd like to avoid having to add it to every single one: [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string FieldName { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string OtherName { get; set; } 回答1: In Startup

.NET Core: Remove null fields from API JSON response

為{幸葍}努か 提交于 2019-12-02 20:02:47
On a global level in .NET Core 1.0 (all API responses), how can I configure Startup.cs so that null fields are removed/ignored in JSON responses? Using Newtonsoft.Json, you can apply the following attribute to a property, but I'd like to avoid having to add it to every single one: [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string FieldName { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string OtherName { get; set; } In Startup.cs, you can attach JsonOptions to the service collection and set various configurations, including removing

How to set .NET Core in #if statement for compilation

断了今生、忘了曾经 提交于 2019-12-02 17:59:52
I created a multi targeted framework project. I use something like this : #if NET40 Console.WriteLine("hello from net 4"); #endif But I cant find wildcard for .NET Core. I tried : #if NETCOREAPP1.0 Console.WriteLine("hello from net Core"); #endif but it is not valid statement. Thanks. Ralf Bönning You need underscore _ instead of point : NETCOREAPP1_0 or the more recent NETCOREAPP1_1 and NETCOREAPP2_0 The article https://docs.microsoft.com/en-us/dotnet/articles/core/tutorials/libraries includes a list for the different preprocessor symbols. .NET Framework 2.0: NET20 .NET Framework 3.5: NET35

ASP.NET Core: asp-* attributes use request payload over model?

孤者浪人 提交于 2019-12-02 01:03:42
问题 It seems that in ASP.NET Core, the value in asp-* attributes (e.g. asp-for ) is taken from the request payload before the model. Example: Post this value: MyProperty="User entered value." To this action: [HttpPost] public IActionResult Foo(MyModel m) { m.MyProperty = "Change it to this!"; return View(); } OR this action [HttpPost] public IActionResult Foo(MyModel m) { m.MyProperty = "Change it to this!"; return View(m); } View renders this: <input asp-for="MyProperty" /> The value in the form

How to use PCL in ASP.NET Core 1.0 RC2 project

情到浓时终转凉″ 提交于 2019-12-01 21:50:36
I want to use a simple (POCO only without other dependencies) PCL with targets Xamarin.Android Universal Windows 10 ASP.NET Core 1.0 in an ASP.NET Core 1.0 RC2 project (all in the same solution). I have added this import to my project.json: "frameworks": { "netcoreapp1.0": { "imports": [ "dotnet5.6", "dnxcore50", "portable-net451+win8" ], "dependencies": { "PCL.Library": { "target": "project" } } } } I don’t get any errors (compilation or otherwise) and the PCL.Library project shows up in the references (without warning signs or anything). But I can’t use it in my ASP.NET Core 1.0 RC2 project

Cookies in ASP.NET Core rc2

家住魔仙堡 提交于 2019-12-01 21:08:22
Can someone pls explain how to store and get cookies in an ASP.NET Core rc2 application? I can only find outdated information about the old HttpContext.Response.Cookies.Get and Add methods, neither of which still exist in Core. Also, the HttpCookie class doesn't seem to exist either. What is the new cookie class and how can I get and add one? (Note: I am not specifically taking about authentication cookies, just general data cookies) For getting request cookie value: HttpContext.Request.Cookies["<key>"] Setting response cookie: HttpContext.Response.Cookies.Append("<key>", <value>, <options?>)

Filtering Out Properties in an ASP.NET Core API

爷,独闯天下 提交于 2019-12-01 18:07:14
I want to serve up the following JSON in my API: { "id": 1 "name": "Muhammad Rehan Saeed", "phone": "123456789", "address": { "address": "Main Street", "postCode": "AB1 2CD" } } I want to give the client the ability to filter out properties they are not interested in. So that the following URL returns a subset of the JSON: `/api/contact/1?include=name,address.postcode { "name": "Muhammad Rehan Saeed", "address": { "postCode": "AB1 2CD" } } What is the best way to implement this feature in ASP.NET Core so that: The solution could be applied globally or on a single controller or action like a

InvalidOperationException when registering a new user with ASP .NET Core Identity and EntityFrameworkCore

社会主义新天地 提交于 2019-12-01 17:41:13
问题 I'm following the documentation for using Identity and am trying register a new user (executing the register action), but it fails with the following error: InvalidOperationException: Cannot create a DbSet for 'ApplicationUser' because this type is not included in the model for the context. Startup: services.AddIdentity<ApplicationUser, IdentityRole>(options => { //password options options.Password.RequireDigit = false; // ... }) I am using a standard ApplicationUser: public class

WCF .NET Core - WsHttpBinding Configuration project.json

杀马特。学长 韩版系。学妹 提交于 2019-12-01 17:39:39
问题 How do I configure wsHttpBinding with .NET core, Should I configure in Project.json file ? Before .NET Core the configuration is like below <system.serviceModel> <bindings> <wsHttpBinding> <binding name="IService_Endpoint"> <security mode="None" /> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://serviceURL/Service.svc" binding="wsHttpBinding" bindingConfiguration="IService_Endpoint" contract="IService" name="IService_Endpoint" /> </client> I found an article which

ASP.NET Core DisplayAttribute Localization

旧城冷巷雨未停 提交于 2019-12-01 16:59:30
According to the documentation : The runtime doesn’t look up localized strings for non-validation attributes. In the code above, “Email” (from [Display(Name = "Email")]) will not be localized. I'm looking for a way to localize text in DisplayAttribute. Any suggestions to do it in a proper way(s)? You can set the ResourceType on the DisplayAttribute which can be used to localize your text. Add a resource .resx file to your project e.g. MyResources.resx , and add a resource for your field: Then reference the name of the field and the MyResources type in your DisplayAttribute [Display(Name =