How To Fix Archives - Digital Tool Factory blog

The Digital Tool Factory Blog

How to post objects directly to web api from C#

As I’ve looked this up three times now I should put the code directly on the blog. Happily Microsoft has done a lot of the plumbing work for you.

You have a Web API controller with an action that looks like this
[System.Web.Http.Route("Ajax/v1.1/PostItemBatch")]
[OutputCache(NoStore = true, Duration = 0)]
[System.Web.Http.HttpPostAttribute]
public bool PostItemBatch(List postItems)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
foreach (PostItem postItem in postItems)
{
db.PostItems.Add(postItem);
}
db.SaveChanges();
return true;
}
}

Then you can post data to it by the following

List PostItems = new List();
for (int i = 0; i < 11; i++)
{
PostItems.Add(new PostItem(true));
}

HttpClient client = new HttpClient();
HttpResponseMessage response = client.PostAsJsonAsync("http://localhost:56168/Ajax/v1.1/PostPostItemBatch", PostItems).Result;
var test = response.IsSuccessStatusCode;

And there you go!


15
Oct 18


Written By Steve French

 

How to fix problems with jquery data tables and a list of objects from ASP.net Web API

The main problem is that data tables does not expect a list of objects, instead it wants a differently formatted json object, something like

data: {“Property”: Value}

etc
Instead it’s getting a pure list, like this

[
{“Name”:”Value},
{“Name”:”Value},
{“Name”:”Value},
{“Name”:”Value},
{“Name”:”Value},
]

Solution – list out the values in the columns property of the data table, like this

$(document).ready(function () {
$(‘#example’).dataTable({
“deferRender”: true,
“processing”: true,
“iDisplayLength”: 25,
“ajax”: {
“url”: “https://domain.com/Ajax/v1.1/ListTypes”,
“dataSrc”: “”
},
“columns”: [
{ “data”: “Name” },
{ “data”: “OtherColumn” }
]
});
});

And you’re done!


10
Oct 18


Written By Steve French

 

How to fix problems with conflicting version of Newtonsoft.Json in your visual studio projects

The root problem seems to be in the version of Newtonsoft.json – if you set it to version 11.0.1 all of the problems magically disappear

 

 


24
Aug 18


Written By Steve French

 

How to fix the entity framework problem with System.Data.Entity.Migrations.Utilities.DomainDispatcher not found.

After much searching and repair, I could not find a good way – the error does seem to be documented on Github – so at least there’s that.

My solution to the problem, after trying several other ways, was to downgrade Visual Studio as described here – note – you really do have to run all of the cleanup tools.

UPDATE ON 09/21/2018 – this fixes the problem – thank you Stack Overflow User.

Copy and paste this

<dependentAssembly>
<assemblyIdentity name=”System.Management.Automation” publicKeyToken=”31bf3856ad364e35″ />
<bindingRedirect oldVersion=”0.0.0.0-3.0.0.0″ newVersion=”3.0.0.0″/>
<publisherPolicy apply=”no” />
</dependentAssembly>


24
Aug 18


Written By Steve French

 

The simple way to force redirect to https in web config files

There are two things:
1. Just insert this code in the web.release.config

<system.webServer xdt:Transform=”Insert”>
<rewrite>
<rules>
<rule name=”Force HTTPS” enabled=”true”>
<match url=”(.*)” ignoreCase=”false” />
<conditions>
<add input=”{HTTPS}” pattern=”off” />
</conditions>
<action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}” appendQueryString=”true” redirectType=”Permanent” />
</rule>
</rules>
</rewrite>
</system.webServer>

2. Then make sure that the Url Rewrite module is installed in IIS – if that module is not installed you will get perplexing, unhelpful error messages


24
Jul 18


Written By Steve French

 

How to fix the directory name is invalid IIS problem

Just restart the app pool and the problem goes away.


22
May 18


Written By Steve French

 

How to fix the problems with the package manager console in Visual Studio 2017

So – you’re getting the following problem

The following error occurred while loading the extended type data file: Microsoft.PowerShell.Core, C:\Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(3763) : Error in type “System.Management.Automation.FormatViewDefinition”: Exception: The getter method should be public, non void, static, and have one parameter of type PSObject.

For some reason this just started happening after the last Visual Studio 2017 update

Update – 5/29/2018 – make sure to leave VS Code open while you open Visual Studio 2017 – the problem does recur after every restart for some reason.

First – locate devenv.exe.config in the C:\Users\WarHorse\AppData\Local\Microsoft\VisualStudio\15.0_YOURIDENTIFIER directory

Open that file in VS Code

paste the following in with the other dependent assemblies

<dependentAssembly>
<assemblyIdentity name=”System.Management.Automation” publicKeyToken=”31bf3856ad364e35″ />
<publisherPolicy apply=”no” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=”Microsoft.PowerShell.Commands.Utility” publicKeyToken=”31bf3856ad364e35″ />
<publisherPolicy apply=”no” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=”Microsoft.PowerShell.ConsoleHost” publicKeyToken=”31bf3856ad364e35″ />
<publisherPolicy apply=”no” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=”Microsoft.PowerShell.Commands.Management” publicKeyToken=”31bf3856ad364e35″ />
<publisherPolicy apply=”no” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=”Microsoft.PowerShell.Security” publicKeyToken=”31bf3856ad364e35″ />
<publisherPolicy apply=”no” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=”Microsoft.PowerShell.Commands.Diagnostics” publicKeyToken=”31bf3856ad364e35″ />
<publisherPolicy apply=”no” />
</dependentAssembly>


16
May 18


Written By Steve French

 

How to fix the The type initializer for Emgu.CV.CvInvoke threw an exception problem when deploying to azure web apps

I’ve been doing a little Facial Recognition lately – then came across the following error “The type initializer for ‘Emgu.CV.CvInvoke’ threw an exception.” after I deployed to azure web apps.  Everything was working fine locally.

After doing more digging I realized that the actual .exe files for open cv were not being included in the web deployment – all that was necessary was to include the x86 and x64 directories in the project in the Solution Explorer in the project and presto!  Everything works wonderfully.


04
Jul 16


Written By Steve French

 

How to fix the System.InvalidOperationException: The property ‘PropertyID’ is part of the object’s key information and cannot be modified problem

Often times when you are using Entity Framework and trying to update objects using the handy

db.Entry(useableProperty).CurrentValues.SetValues(property);

Method you will encounter the error

InvalidOperationException: The property ‘PropertyID’ is part of the object’s key information and cannot be modified problem

After lots of thought I came across the remarkably simple answer, to wit – the PropertyID field is not being EXPLICITLY included in the object you are passing back to the controller.  Just put that in a “HiddenFor and you’re all set.


02
Nov 15


Written By Steve French

 

How to fix an odd problem with autonumeric.js

So, you’re using the standard autonumeric.js jquery plugin on your html 5 site – you set up the initialization properties and nothing happens – what is wrong?

First check to make sure that your textbox is of type “text” and not type “number” – that threw me for about ten minutes today.


08
Apr 15


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.