ASP.net MVC Archives - Digital Tool Factory blog ASP.net MVC Archives - Digital Tool Factory blog

The Digital Tool Factory Blog

A quick write up to document web config changes when deploying to azure using release pipelines in azure devops

So – you want a step by step guide to deploying web apps to azure web apps from azure devops and changing config files in the release pipelines? Look no further.

First create the new web.config files, as seen below

In the file properties – make sure they are set to “Content” and “Copy Always”

In the pipeline properties – make sure that xml transformation is checked, and that there is the proper value of
-ASPNETCORE_ENVIRONMENT ProdAzure

In Make sure your azure stage has the same name as the web.{name}.config file


04
May 22


Written By Steve French

 

How to Get the ID of the logged in user in asp.net mvc web api apicontroller

After much googling, and evening Bing-ing revealed nothing I discovered that for some reason I had the line

config.SuppressDefaultHostAuthentication();

In my WebAPIConfig.cs file – I removed that and everything magically worked and I could use User.Identity.GetUserId();


28
Feb 22


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 properly clone a project from Visual Studio.com

So – I recently had to clone a site off of Visual Studio.com lately – after some discovery, here are the proper steps

  1. Don’t try to do it from Visual Studio 2017 directly – instead just go to the project in VisualStudio.com, and click “Clone Is Visual Studio” – a dialogue window will pop up, and the combination f Visual Studio 2017 and Visual Studio.com will take care of everything for you
  2. Visual Studio will then be open to the FOLDER of the newly created project.  Note the folder location, then close Visual Studio, and open the solution (.sln) file in that folder (why Visual Studio doesn’t open it immediately I don’t know)
  3. From there, you have to copy over the nuget packages – in the nuget package manager, just enter “Update-Package -reinstall” and they will all be reinstalled or installed (that solution found here – the packages are not copied into source control)

and you’re done!


22
Jan 18


Written By Steve French

 

The peculiar problem when your entity framework query returns the same row for every record

This one had me confused for a long time, several hours in fact.  There were over 8,000 rows in the table, and the for some reason the query was returning the same row 8,000 times.  After much rending of garments and gnashing of teeth, I discovered that the table in question (in a sql server 2000 database (not created by me)) had no primary key defined.

So – I copy the data into a new table, create a key with the commands

alter table TableInQuestion
add NewIDColumn int identity(1,1)

Delete the empty key field (that was what threw me, there was a column named “Id”, which was not actually and ID column.) rename “NewIDColumn” to “Id”, then point EF to the new table with the data attribute (on the object)

[Table(“TableInQuestion”)]

and everything works like a charm

 


22
Sep 17


Written By Steve French

 

How to fix problems with password reset tokens

For some reason, asp.net identity 2 generates password reset tokens that are not property url encoded, but does send them as part of a querystring.  Therefore – when you send out the email – the web browser will add spaces to the code, which renders it invalid.  The way around that is to base 64 encode (and decode) the code as it is sent and interpreted.  Below are the Base64 extension methods.

Also – make sure there is a guid (any guid) in the SecurityStamp column in the AspNetUsers table – that will also cause it to claim that the code is invalid.

public static string ConvertToBase64(this string str)
{
byte[] encbuff = Encoding.UTF8.GetBytes(str);
return HttpServerUtility.UrlTokenEncode(encbuff);
}

public static string ConvertFromBase64(this string str)
{
byte[] decbuff = HttpServerUtility.UrlTokenDecode(str);
return Encoding.UTF8.GetString(decbuff);
}


10
Jun 17


Written By Steve French

 

How to do a cross domain json request with jquery and asp.net mvc web api

So – I was trying to request some data from one server from another – not normally a big deal, but the data would vary depending on whether or not the user was logged in or not.  I thought just setting up CORS would work (I’m using asp.net mvc web api 2).  I thought a simple jquery .post or .get would do the job, but surprisingly the .get and .post do not send the auth cookie when making the request – you have to use the .ajax features of jQuery, as well as enabling the “SupportsCredentials” part of Cors – the relevant parts look like this

In your web api controller

[EnableCors(origins: “*”, headers: “*”, methods: “*”,  SupportsCredentials = true)]

Your javascript code should look like this

$.ajax({
url: ‘http://MyWebServiceDomain.com/api/v.1.1/SomeController/MyFeature’,
dataType: ‘json’,
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (authText) {
$(‘#authText’).html(authText);
}
});


19
Jan 17


Written By Steve French

 

How to fix 404 errors in asp.net web api 2

So – you’ve tried everything and you’re still getting weird 404 errors in asp.net web api 2.    You’ve tried the 4 main ways of fixing it, and you still get nothing – that was what I did anyway.  Finally I started a new project in the solution, and noticed that the problem only occured when other projects were referenced – and that I had several duplicate controller names.

I.E. Project A had and “AjaxController” and Project B had an “AjaxController”.  Everything works fine until you reference Project A from Project B – if you change the name of the controller in Project B the problem goes away and everything works perfectly.


26
Sep 16


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 problems with Jquery Validate and dynamically generated forms

I recently came across the problem of validating dynamically generated forms with the jquery.validate plugin – everything worked well with the original form, but when there were multiple forms available I got peculiar syntax errors. A quick googling told me to simply add the following code

$(“form”).validate();

to the javascript and everything would be fine. I tried that to no avail. Then I read things more closely and realized that I needed to specify which form I wanted to validate, I changed the code to

$(“#editForm”).validate();

And voila! Problem so.


03
Mar 15


Written By Steve French

 




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