Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, 12 November 2012

LINQ, Counts and Group By

After a particularly busy weekend full of late nights working remotely towards a looming deadline, I was having trouble with writing a LINQ query that would return me values based on a GROUP BY clause. What I basically wanted was:

SELECT COUNT(Id)
FROM Table
GROUP BY UserId

I had no idea what the LINQ GroupBy() method actually returned, but this article helped me figure it out.

Table
  .GroupBy(x => x.UserId)
  .Select(g => g.Count());

Or, even more handy:

Table
  .GroupBy(x => x.UserId)
  .Select(g => new { userId = g.Key, count = g.Count() });

Thursday, 8 November 2012

Application level error handling

On the project I've been working on, we've been asked to log any exceptions that are thrown by the site. There are packages out there (like ELMAH), but in case you want to add some form of custom logging (or extra handling), there's a useful chunk of sample code for doing just that. :)

Tuesday, 30 October 2012

Sessions not persisting for Facebook app in Safari

I had an issue recently with a Facebook app (using .Net in the back end) in Safari. Nothing put into the user session was persisting between page loads. The user was authenticated, put into the session, but on the next page bam - gone again.

After doing some checking, I found a few posts that pointed me in the direction of the issue. Turns out that Safari considers the app inside the iframe to be a "third-party" site and therefore doesn't accept the session cookie. More searching turned up a suggestion - open the app site outside of the iframe if you're in Safari so that the cookie can be set.

So that's exactly what I did. During the authentication stage of the app (when dealing with the oauth token and so on), I check to see if the Request.UserAgent contains "Safari" (and not "Chrome"). If so, I redirect to a new view, SafariRedirect which uses a chunk of JavaScript to force my Facebook Authorisation view to open in the top frame.

This view sets the session cookie by putting a dummy value into it, allowing the cookie to exist for the site going forward (including within the iframe).

And that managed to do the trick.

Monday, 2 January 2012

Fetching user location using their IP address thanks to the IPInfoDB API

So, I needed to find a way of fetching a website user's location using their IP address.  A quick Google search found this stack overflow thread that mentioned a few options.  I decided to try out the IPInfoDB API because it's free and seems to do exactly what I wanted.

The code (bear in mind I'm using C#) what pretty simple to write:

I then stored the information in the session (so that I wouldn't need to make lots of requests to the service).

And there you go.  Geo-location information from the IP address.  The API goes down to city level, which is handy, although as expected the location isn't 100% accurate.  My home IP returns my location as being several miles away, but at least it's in the right city  :)