ASP.net Archives - Digital Tool Factory blog ASP.net 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

 

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 file creation and web deployment in asp.net

The scenario – you have an asp.net app that creates a file of some kind – everything works great – then you upload it and the file is not created.

It turns out that Visual Studio will not deploy an empty folder, or a folder that it seems as empty anyway – if the app is creating the files, the new files will not be visible to Visual Studio.  Visual Studio will see an empty folder and not create it on the server.  You will run the app, and it will try to save a file to a folder that isn’t there, and will create an error.

The easiest way to do that is to just create a blank text file in the folder.  Visual Studio will see that file, and then upload the text file (in doing so creating the folder) and the app will have a folder to create the new file in.


05
Jun 17


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

 

How to track outbound links in the new version of Google Analytics

So the Gods of Google have gone and changed their basic Google Analytics code to something that works slightly better, and now your outbound link tracking code doesn’t work.

Here is how you fix that – first make sure you have the most recent (as of July 2013 Google Analytics code, it looks something like this)

<script>
(function (i, s, o, g, r, a, m) {
i[‘GoogleAnalyticsObject’] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, ‘script’, ‘//www.google-analytics.com/analytics.js’, ‘ga’);

ga(‘create’, ‘UA-XXXXXXXX-1’, ‘mydomain.com’);
ga(‘send’, ‘pageview’);

var clientId = ”;
ga(function (tracker) {
clientId = tracker.get(‘clientId’);
});
</script>

 

If it does not look more or less like that, then do not use the code below.

Now make the links you want to track look  like this

<a href=”http://www.SomeOtherDomain.com” onClick=”ga(‘send’, ‘event’, ‘Outbound Links’, ‘click’, ‘http://www.SomeOtherDomain.com’)” target=”_blank”>www.SomeOtherDomain.com</a>

Note the onclick event – that is the key thing.

 

Where can you see the result?  They do not show up as page views  (like they used to) instead they are treated as “Events”.  To see them, go to (in the main Google Analytics screen)

1.  Standard View

2. Content

3.Events

4. OverView

5. Event Label

And there you are.  Not exactly intuitive, but not bad.

 


08
Jul 13


Written By Steve French

 

Tips and Tricks for running WordPress on Windows Azure

'factory at night' photo (c) 2008, Kazue Asano - license: http://creativecommons.org/licenses/by/2.0/I recently decided to experiment with Windows Azure, and here are some lessons learned about running WordPress on Windows Azure.  I wish I’d known these things before I started.

  1. Getting an install of WordPress on Windows Azure is as simple as they say it is.  Moving an existing site over to it can be challenging.
  2. One thing they do not tell you – while you can run up to 100 Azure Websites, which could be WordPress, on a reserved Azure instance, you only get 1 MySql Database.  As in one for the whole account.  The one you do get seems to be very nice.  When I first discovered this I almost scrapped the idea, but then I came across this blog post on using one MySql database for multiple WordPress accounts.  All you have to do is change the line

    $table_prefix = ‘wpGP_’;

    and you’re all set

  3. You cannot change the maximum upload size – it is set at 2 megs and that is all you get.  Since you are more less running in shared environment, that is not a bad thing
  4. However, I was moving my personal blog, which has been running for over seven years, and my initial import file was 14 megs in size.  I fixed this by splitting the 1 xml file into 19 xml files.  Tedious, but not difficult.
  5. Once all of the posts were in place it was a simple matter of migrating plugins and settings.  Quite frankly I was thrilled with the performance and responsiveness.

All good so far, but then I ran into the largest problem, specifically CNames.  Windows Azure uses CNames for EVERYTHING, which should not be a problem, but I hadn’t gotten around to moving that domain off of GoDaddy yet.  While it was easy to add a CName for www.moodyloner.net, it was NOT possible to add a CName for moodyloner.net.  This seems to be by design on Godaddy’s part.  I’m not sure why.  You can add a redirect of the domain in addition to changing the CName which is what I did.  Then you wait for two hours.

About a half hour in I lost my patience and moved the DNS over to Zonomi.  I was able to set everything up about five minutes and now I have a happy, responsive blog in the cloud.

Final thoughts about running WordPress on Windows Azure

I really like it.  The site is nice, fast, and responsive.  The permissions are configured properly out of the box and, now that I know all of these caveats, setting everything up should be easy going forward.


17
Sep 12


Written By Steve French

 

How to fix Visual Studio slowness problems by deleting ExpansionsXML.xml

The Problem

'Program Guy 1' photo (c) 2009, Briles Takes Pictures - license: http://creativecommons.org/licenses/by/2.0/I’ve had Visual Studio 2010 installed from the first day it was released, and lately it has gotten significantly slower.   That’s not surprising with an increased number of add-ons and extensions.  However, closing the program recently started taking an increasingly long period of time.  It recently reached OVER EIGHT MINUTES so I opted to do some googling.

The Cause

I started by installing PerfMon, and PerfMonWatson, which while telling me the computer was slow, did nothing to tell me of the nature of the problem.  Then I installed Process Monitor by the Sys Intenals division of Microsoft and watched what happened when I opened adn closed the Visual Studio 2010.

The result?  When you open or close Visual Studio 2010 a terrifying number of things happen, but in my case an xml,

C:Users[UserName]AppDataLocalMicrosoftVisualStudio10.01033ExpansionsXML.xml

was being accessed, read and closed several thousand times.  Apparently Visual Studio will do that when the file gets corrupted.

The Solution

Just delete the .xml file and everything should run normally.  Visual Studio 2010 regenerated the file, and it was about one fifth the size it was before.  Everything is working slightly faster in general, and it now closes at a normal speed.


22
Jun 12


Written By Steve French

 

How to programatically set the sql server database used by entity framework

The Problem:

'Entity Framework' photo (c) 2009, SondreB - license: http://creativecommons.org/licenses/by/2.0/For one reason or another you need to set the database used by entity framework programatically.  The more common way of doing such a thing is to set the database in your web.config class, but for whatever reason you need to set it in actual C# code.

The Cause:

There is no cause really, it is merely the way that entity framework and sql server work together.

The Solution:

The answer lies in setting the constructor properly.

Example Code Using Entity Framework and Data Context

using System.Data.Entity;

namespace MyDBServerExperiments.Models
{
public class DTFContext : DbContext
{
public DTFContext() : base(“Data Source=MyDBServer.DBServer.com;Initial Catalog=MyEntityFrameworkDB;Persist Security Info=True;user id=MyUsername; Password=MyPassword;”) { }

public DbSet<User> Users { get; set; }
public DbSet<BusinessObjects> BusinessObjects { get; set; }
public DbSet<Property> Properties { get; set; }

}

}

The above would be your data context file that uses the DBContext file  in your models folder, just fyi (that is the common place for Entity Framework models in the standard  Visual Studio 2010 web solution structure).

Note, everything happens in the constructor.  This give you many interesting opportunities to programatically swap out Sql Server database names and users, compared to the web.config option, which tends to be far more static.  It allows far more extensive use of business objects and


24
Apr 12


Written By Steve French

 

How to use UI Hints and Display Templates in ASP.net MVC 3

'wtf - code quality measurement' photo (c) 2008, smitty42 - license: http://creativecommons.org/licenses/by-nd/2.0/Surprisingly there is very little information about UI Hints and Display templates for ASP.net MVC 3, so I thought I would share what I have learned.

What are Display Templates?

Display Templates are lovely features of ASP.net MVC 3 that allow you to have a common formatting for certain properties of your objects.  For example, take the belowp roperty (which should be located in your models folder)

[DataType(DataType.ImageUrl)]
[UIHint(“ImageUrl”)]
[Display(Name = “Photo”)]
public string Photo { get; set; }

Everything looks like a standard property, UIHint property.  It would correspond to the Display Template (which is below, it would be name “ImageUrl.cshtml” and would be located in your Views/Shared/DisplayTemplates folder)

@model string
<img style=”display: block;” src=”@Model” alt=”” align=”right” />

What does the UI Hint do?

The UI Hint tells ASP.net MVC 3 to always display the Photo property using the formatting in the ImageUrl.cshtml file.  That way everything is nice, clean and using much less code.

How do you call UI Hints and Display Templates?

Just use the Html.DisplayFor command, for example the Display template below would look like this in Visual Studio 2010

@Html.DisplayFor(xx => xx.Photo)

and would render as

<img style=”display: block;” src=”/img/Photos/Photo123.png” alt=”” align=”right” />

in the browser, allowing you to keep formatting and style consistent across your entire site with very little effort.

 


10
Apr 12


Written By Steve French

 

How to fix the Initialization method TestInitialize threw exception problem in In Visual Studio 2010 Unit Testing

The Problem

You are making an ASP.net MVC 3 web application, and being a good person, you are unit testing as much as possible.  You have properly separated your database from your business objects and every other good practice.  However, when you unit test your controllers, you throw in a fake DBContext (I am assuming you are using the Entity Framework and Moq) and all of a sudden, you get the following error

<b>Failed 1 tests.</b>
Initialization method [YourTestClass].TestInitialize threw exception. System.ArgumentNullException: System.ArgumentNullException: Value cannot be null.
Parameter name: source.

Which certainly makes for one of the least helpful error messages of all time.

The Cause

The problem seems to be lazy loading and the DBSet Feature of Entity Framework.  For example the below passage in your source code below will throw the error

public virtual DbSet<Setting> Settings { get; set; }
public virtual DbSet<AccountSource> AccountSources { get; set; }

This bit of code will not thow the error

public DbSet<Setting> Settings { get; set; }
public DbSet<AccountSource> AccountSources { get; set; }

The problem seems to be entirely in the initialization method.

The Solution

Rather irritating isn’t it?  Especially since Lazy Loading is the design pattern to use, and works great in every other case.  However, if you just delete the “virtual” keyword everything works as intended.

 


03
Apr 12


Written By Steve French

 




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