Tuesday, September 27, 2011

URL Routing

URL Routing is so much easier in .NET 4.0. A quick overview of routing is basically where you would pass values to a folder structure instead of parameters in the url string.

For example:
http://website.com/food/burgers

instead of:
http://website.com/food/foodItemCateogry.aspx?CategoryID=3

The first example is much nicer for SEO reasons, makes it easier to remember, and also makes the site a little less "hackable". I don't really know how else to explain it but here's an article that explains it all.

http://msdn.microsoft.com/en-us/library/cc668201.aspx

I recently had to implement this on a site I was working on and it worked great, and it was SUPER easy in .NET 4.0.

Thursday, September 22, 2011

Using JQuery to Crop Images

Below is a great article I found on how to use jquery and jCrop to crop images. I recently had to use this and it turned out great. The code itself was very easy to implement and I had cropping/uploading in a matter of hours.

http://www.mikesdotnetting.com/Article/95/Upload-and-Crop-Images-with-jQuery-JCrop-and-ASP.NET

A Trick to Deal With Image Cacheing

Let's paint a very common scenario. Suppose you have to upload images to a folder for your web application. You upload your image and it looks great. Then, suppose you have to upload an additional image, and you want to name it the same thing. The problem you'll inevitably face is that because of how browsers cache images, it's very likely that you see your old image again, even though you know for certain that it's been replaced by your new image in the folder. This can get quite frustrating!! Well, there's a very easy workaround to this. All of you have to do is append a random string to the imageUrl, and the browser will think of it as a different image, and not display the cached image. Here's an example, of an image I have displayed in a DataGrid:

protected void AdminGrid_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

System.Web.UI.WebControls.Image imgPreview = (System.Web.UI.WebControls.Image)e.Row.FindControl("imgPreview");

Random rand = new Random();
imgPreview.ImageUrl = "../Images/Products/" + product.ImageName + "? rand=" + rand.Next().ToString();

}
}


I use the Random class to generate my random string which I append to my imageUrl, and now cacheing will never apply to this particular image. Easy peasy, lemon squeezy.

Tuesday, September 20, 2011

Great LINQ Examples

I found a great reference site with LINQ statements on it. I check here often for samples when I'm trying to figure out how to do something.

101 LINQ Samples

By the way, I LOVE LINQ. LINQ will give you intellisense on your queries, and it allows you to do your filtering and sorting in memory. I've also found that developing in LINQ is much faster because you end up going back to the database less, and focus on your code more. Critics of LINQ will say that LINQ adds additional overhead and will cause your performance to suffer, which is probably true, but in most cases the difference is marginal, and for me, the benefits outweigh the cost. Like any tool or technology out there, the key is understanding when to use what, and not just blindly saying that something sucks or something is great for everything. This is rarely the case.

Monday, September 19, 2011

Using ASP.NET Membership Tables

ASP.NET Configuration page couldn't read security settings, wha???

I had this problem after re-creating a database that I built, that happened to use the ASP.NET membership tables. For some reason, when using ASP.NET Configuration to manage my roles and security settings, the configuration page had trouble reading the fresh schema I just generated. It turns out, it's because I need the following values in aspnet_schemaversions:

Feature | CompatibleSchemaVersion | IsCurrentVersion
common 1 1
health monitoring 1 1
membership 1 1
personalization 1 1
profile 1 1
role manager 1 1

These values are usually entered into the membership tables by default when you generate them the first time, using .NET's scripts! This is helpful to know.

Profile Customization

I had a lot of trouble trying to figure out why I couldn't add profile properties to my user registration in ASP.NET 4.0. All of the blogs and searches I found said it was easy - all you have to do is add a few tags to your web.config, and then a custom class should be generated for you on build, and "voila!", so easy!

The problem was, no matter how I messed with the web.config to add those profile property tags, it seemed like I could never see this auto generated custom class. I dug a little deeper and found out that the reason why I couldn't use this feature, is because it doesn't work with Web Projects!! It only works with ASP.NET Web Sites. This is a distinction that many people overlook. Anyhow, there is a way to do it, and I've posted the very helpful link below.

http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx