Digital Tool Factory blog - Backend web development in Atlanta GA

The Digital Tool Factory Blog

How to add extra parameters to post requests on asp.net web api 2

My only problem with asp.net web api 2 is the inability to pass additional parameters the action, along with a base object. For example – if you want to update a contact, but keep track of where the update request came from (the location is not really part of the contact object) you cannot just add location as an additional parameter in addition to contact.

The only way of doing this is to add a parameter to the route – for example

[System.Web.Http.Route(“Ajax/v1.1/{locationArea}/SomeContactAction”)]
[OutputCache(NoStore = true, Duration = 0)]
[System.Web.Http.HttpPostAttribute]
public bool IsUniqueEmail(string locationArea,Contact contact)
{
//stuff happens
DoStuff(contact, locationArea);
return bool;
}


12
Apr 19


Written By Steve French

 

How to fix problems with entity framework migrations

  1. First – backup the __MigrationHistory table in the database
  2. Get rid of old migration files
  3. add-migration using the usual syntax, but with nothing modified
  4. Remove all of the “Create Table”, etc – the migration should look like
  5. public partial class domorenow : DbMigration
    {
    public override void Up()
    {

    <code>    }
    
        public override void Down()
        {
    
        }
    }</code>
  6. from there do the update-database -script to generate a sql file
  7. then just update-database -verbose
  8. All Fixed

14
Feb 19


Written By Steve French

 

Is your Windows 10 PC slow when connected to the router, but fast when plugged into the modem?

I recently had this problem, and after getting a new router, and a visit from Comcast, the problem turned out to be a setting, specifically the QOS Packet Scheduler. I have no idea how that setting got turned on, but turned on it was. It reduced my connection from 290 megs per second to 4.5. Quality of Service indeed. All I had to do was uncheck that box and it everything was fine again. I guess Windows 10 uses different settings for the router vs direct connection to the modem.


28
Jan 19


Written By Steve French

 

Why is my m.2. ssd slow on Windows 10? Read on, I fixed it

So, I installed a new Samsung SSD M.3 drive and found it to be much, much slower than it’s predecessor. After much sturm, drang and gnashing of teeth I stumbled across the solution. The key is in the power requirements on all things – basically just change the power profile to “Performance” in the settings and then confirm that PCI-E is set to 100%. After that it is a night and day difference.


17
Dec 18


Written By Steve French

 

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.

Update on 05/02/2019 – this affects Visual Studio 2019 as well – one thing to note, this is the path to the new file
C:\Users\WarHorse\AppData\Local\Microsoft\VisualStudio\16.0_181cd91f

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

 

A simple little linq values transformer

Somehow I’ve never actually used this keyword before, but as I gradually shift over to functional programming I came across the ForEach linq statement – for example

listOfObjects.ForEach(x => x.BodyText = x.BodyText.Replace(“,”,”, “));

And bam – all of the body text is replaced without having to loop through the enumerable.  Rather nice.  A simple little statement, but one that I’ve never really had the change to use before.


13
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

 




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