I started porting our .Net Framework WebAPIs to .Net Core 5 at work. I noticed that dates being returned in the SPA were off and upon further investigation I saw that after the objects were serialized that the dates were being set as UTC.
I've found that I can get it to work at serialization/deserialization by adding the option as an argument, but it seems like this should be able to be done globally instead of each time the serializer runs.
I've tried adding the following in the WebAPI Startup.cs to change how the dates were being returned but it doesn't seem to work in the startup:
services.AddControllers().AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified);
Add this:
services.AddControllers().AddNewtonsoftJson();
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
};
I had to convert to UTC format(DateTimeZoneHandling.Utc) and the above ways(DateTimeZoneHandling) did not work for me. Finally below worked:
.AddNewtonsoftJson(opts =>
{
opts.SerializerSettings.Converters.Add(new IsoDateTimeConverter
{
DateTimeStyles = DateTimeStyles.AdjustToUniversal
});
}
I hope this helps to people who are looking to convert date time format to UTC globally.
Related
I had some code using the RoleManager that I had copied from an MVC project to create roles in a Blazor project.
It compiles okay but when I step through the code it gets to that line and then appears to throw and exception, as it doesn't ever go to the next line. I am unable to catch an exception though.
Since my code didn't work I found code on the web and it behaved the same, so I changed how it was injected and I got a different error, so I found a third way of doing it and that too gets to the line that tests to see if there is a role already existing and blows up.
Here is the latest attempt
[Inject]
IServiceProvider services {get;set;}
public async Task Initialize()
{
using (var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>())
{
string proRole = "Pro";
if (!await roleManager.RoleExistsAsync(proRole))
{
var temp = await roleManager.CreateAsync(new IdentityRole { Name = proRole });
}
}
}
And it blows up on the awaited if statement.
Here is the previous code that should work that doesn't.
string proRole = "Pro";
string proClientRole = "ProClient";
if (!await _roleManager.RoleExistsAsync(proRole))
{
var temp = await _roleManager.CreateAsync(new IdentityRole { Name = proRole });
}
which is inside of a method.
I also had it as #{code in the Razor file and after an hour of trying different things there I moved it to a base class because they have been more stable in the past.
It is not the database connection because that is well verified and also because UserManager is called in the same class and it work
I also tried this code (Blazor Role Management Add Role trough UI (Crud)) both suggestions.
This is one bad thing about Blazor for me is it is buggy I never know if it is my bad, or just something wrong with the latest greatest. But I am assuming it is my bad in this case.
Anyway any help would be much appreciated, I am way too many hours on this,
I copy/pasted the same code to the "/" page and it works there.
For some reason it wouldn't work in a component loaded into a component but the standard Role code works as expected in the "/" page.
Most likely I should have restarted my computer as it now works in the original location too.
I'm trying to add a custom middleware to add parameters to the query.
According to some online search, you can do something like this:
{
//PreQueryStringBuilderMiddleware occurs after authorization
PreQueryStringBuilderMiddleware = async (ctx, next) =>
{
var upstreamRoute = ctx.DownstreamReRoute.UpstreamPathTemplate;
Log.Information($"{upstreamRoute}");
await next.Invoke();
}
};
See this answer
But for me it says HttpContext doesn't contain a definition of DownstreamReRoute.
Is there something I am missing or has this been changed?
Alright, turned out this did actually change and in an earlier versions this still works. But it looks like Ocelot is dead anyway from what I read.
var upstreamRoute = ctx.Items.DownstreamRoute().UpstreamPathTemplate
Use above code instead of
var upstreamRoute = ctx.DownstreamReRoute.UpstreamPathTemplate;
Such change was done between 15.0.6 and 16.0.0 Ocelot versions.
For details you can check them on github: https://github.com/ThreeMammals/Ocelot/compare/15.0.6...16.0.0
This example no longer seems to work in .NET Core 2.0, it now serializes using an XmlDiffGram. Any easy way to get it working? Do I need to do this whole thing?
As of 25/04/2018. Download latest version of Newtonsoft. I upgraded to 11.0.2. It now works with ASP Core 2. Datasets get converted to JSON.
It's looks like the Netonsoft crew have written a specific converters for DataSet & DataTables which should point you in the right direction.
Quick update:
It looks like these are not quite in the latest nuget release yet, but coming soon.
when they are released, you'll need to change the line in the example to something like.
string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented, new JsonSerializerSettings { Converters = new[] { new Newtonsoft.Json.Converters.DataSetConverter() } });
Well this works...
var xml = new XDocument();
using (var writer = xml.CreateWriter())
{
dataSet.WriteXml(writer);
writer.Flush();
}
return Json(xml);
I am trying to build a relatively simple web application following tutorials from the book ProAngular. The book examples work fine, but when I try and build my own app, I am getting stuck on a strange error. Here is part of my code:
$scope.dispositionsResource = $resource(dispositionUrl + ":id", { id: "#id" },
{ create: {method: "POST"}, save: {method: "PUT"}, delete: {method: "DELETE"}
});
. . .
$scope.updateDisposition = function (disposition) {
alert("DISPOSITION: "+disposition.name);
disposition.$save();
}
The Create and Delete functions work fine. The updateDisposition method is being called form an HTML form and the correct disposition value is being passed (based on the Alert). But the error I am getting is:
"Error: disposition.$save is not a function"
None of my example code separately defines a save function, the function should be part of the restful service ($resource). Shouldn't it?
Any pointers in the right direction would be greatly appreciated.
Ted
I did end up getting this working. Not totally sure why, but I renamed the Save function to 'Update' and associated it with the PUT functionality.
$scope.dispositionsResource = $resource(dispositionUrl+":id", { id: "#id" },
{ 'create': {method: "POST"}, 'update': {method: "PUT"}
});
$scope.updateDisposition = function (disposition) {
$scope.dispositionsResource.update(disposition);
$scope.editedDisposition = null;
}
calling update rather than save worked. Something seemed to be interfering with using the term 'save'. Like I said, not sure what . . . yet. One of those head-scratchers for me. Thanks to those who tried to assist!
I am learning angular myself, but the first problem I can see with your code is that it doesn't look like you are defining $resource correctly ( fair warning, Angular has a ton of caveats and you may simply be delving into one I am not aware of).
I believe a more straight forward way of doing what you are trying to do is first creating an angular factory for the $resource, like so:
angular.module('yourModuleName')
.factory('disposition', function($resource) {
return $resource('/whatever/url/youwant/:id', {
id: '#id'
})
});
And then from there, declare the factory as a dependency for your controller:
angular.module('yourModuleName')
.controller('yourControllerName', function($scope, disposition) {
$scope.submitForm = function($scope)
disposition.save($scope.nameOfYourModel);
});
One thing to keep in mind is that $resource has all of the methods that you declared by default. From the docs at https://docs.angularjs.org/api/ngResource/service/$resource these are what are available out of the box:
{ 'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'} };
Personally, I prefer to use the $http service myself. Yes, it is quite a bit more verbose than using $resource but I feel that it is much easier to understand $http when starting with angular than the $resource service. To save yourself from a world of headaches in the future, I highly recommend becoming familiar with the concept of promises in Angular as many of its services make use of them.
Good luck.
I recently got an exception from NHibernate when trying to access the Date property of a DateTimeOffset property.
var v1 = nhSession.Query<MyType>.Where(o => o.DateTimeOffsetProperty.Date == DateTimeOffset.Now.Date).ToList();
I would have thought this would have "just worked." What is the best solution to this problem?
I created an HQL generator that does the following in BuildHql:
return p_treeBuilder.MethodCall(
"date"
, new HqlExpression[] { (HqlExpression) p_visitor.Visit(p_expression) }
);
This seems to be working, but I'd like to know if this is already built in and I missed it. Thanks!