January 2013 - Digital Tool Factory blog January 2013 - Digital Tool Factory blog

The Digital Tool Factory Blog

Just added two interviews to the main site

Here are my interviews with Business Radio X.  Enjoy!


23
Jan 13


Written By Steve French

 

How to fix the infamous “Unable to cast object of type ‘System.Int32’ to type ‘System.String’ error in asp.net mvc

The Problem

You have created a lovely model in asp.net mvc 4, using Entity Framework and a whole host of attributes.  One of the properties of your model looks like this

[Required]
[Display(Name = “Expiration Month (MM)”)]
[StringLength(2,ErrorMessage = “You must enter two digits”, MinimumLength = 1)]
[Range(1,12, ErrorMessage = “You must enter a valid month”)]
public int ExpirationMonth { get; set; }

That yields an error of

Unable to cast object of type ‘System.Int32’ to type ‘System.String

When the form is submitted, but not when the object is created (which is a bit odd)

The Cause

It turns out that you cannot use the StringLength data attribute on an int data type.  You would think you could since all you are actually interested in with that data attribute is the number of digits, but the data gods have decreed that you can’t.

The Solution

Just comment out StringLength and use the Range Attribute.


17
Jan 13


Written By Steve French

 

How to fix problems with data cacheing in ASP.net MVC 4, SignalR and Entity Framework

The Problem

I was using the wonderful SignalR libraries to create a status window (I have a VERY long running process in my new Data Hammer project, it takes about 15 minutes to run) and I need to inform the user on where the process is at any given time.  My original code looked like this

public class Chat : Hub
{
public void Send(int id)
{

DataHammerContext ctx = new DataHammerContext();

while (true)
{

string strName=ctx.ProcessSteps.Find(id).Name;
Clients.All.addMessage(“Status: ” + strName);
Thread.Sleep(1000);
}
}
}

And this would always display the same status, quite maddening.

The Cause

After much investigation I realized that my model of how Signal R works was flawed.  For whatever reason (by which I mean too long to go into here) the data context does not update with the while loop.

The Solution

The fix is simple, just recreate the data context every time and everything works like it should – here is a sample of the updated code.

 

public class Chat : Hub
{
public void Send(int id)
{

 

while (true)
{

DataHammerContext ctx = new DataHammerContext();
string strName=ctx.ProcessSteps.Find(id).Name;
Clients.All.addMessage(“Status: ” + strName);
Thread.Sleep(1000);

}
}
}


16
Jan 13


Written By Steve French

 

The free version of Google Apps is going away, but have no fear

A few weeks ago Google decided to end their free version of GMail, or as they referred to it, Google Apps for domains. If you are an existing customer you were grandfathered in, but otherwise you will now have to pay to use their email service (and spam filtering) with your own domain name.

Happily all is not lost. Microsoft recently released their new, free service of Outlook.com. It is a blatant attempt compete with GMail, and quite frankly, it’s great! It is nowhere near as cluttered and ad-centric as GMail and the spam-filtering is equal to Google’s.

Check it out at Outlook.com. The signup – switchover procedure takes all of about 15 minutes.

And on another note, our new, under-promoted Facebook page is quite lonely – if you have a chance, could you take a look and like us?

This first appeared on the Digital Tool Factory newsletter on 12/31/2012


14
Jan 13


Written By Steve French

 

How to configure wildcard routing in asp.net mvc 4

The Problem

You are trying to configure Widlcard routing (i.e. something like http://domain.com/SomeUserName/) and find it problematic.

The Cause

That is largely by design – wildcard routing is resource intensive and rarely needed.

The Solution

If you want to do it anyway, use the following routes

routes.MapRoute(“Default”, “”, new { controller = “Home”, action = “Index”, id = UrlParameter.Optional });
routes.MapRoute(“CatchAll”, “{*id}”, new { controller = “Home”, action = “Handle”, id = “” });

Things to note

 

  • The Catch-All route is AFTER the home route
  • The syntax is *id – this will ensure that the value of ID is sent to the “Handle” action
  • The purpose of this route is to throw control to the “Handle” action with the appropriate value – Handle must be set up to use that value.

 

Okay, after some more digging (and some failed integration tests) here is what you have to do

Add in the following routes

routes.MapRoute(“Handle”, “Home/Handle/{id}”, new { controller = “Home”, action = “Handle”, id = “” });
routes.MapRoute(“User”, “{*id}”, new { controller = “Home”, action = “Index”, id = “” });
routes.MapRoute(“Default”, “{controller}/{action}/{id}”, new { controller = “Home”, action = “Index”, id = UrlParameter.Optional });

And then create an action called “Handler”, and change your Index action on your home controller to look like this

public ActionResult Index(string id=””)
{
if (!string.IsNullOrWhiteSpace(id))
{
return RedirectToAction(“Handle”, new {@id = id});
}
else {
return View();
}

What does this do?  It routes all wildcard traffic to your Index controller, which then hands it off to the “Handle” Action.  You would think that you could just hand everything off to the Handle action, but if you do that, then your Index action becomes inaccessible,  and in some circumstances you get a recursive loop where the handle action is called endlessly.  That does show an interesting error screen though.

One annoying side effect of this course of action is the need to map out each and every route explicitly, otherwise the wildcard route will pick it up and hand it off to the “Handle” action, which will take it somewhere where you do not want it to go.


04
Jan 13


Written By Steve French

 




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