ASP.NET Web Pages code behind and seperation of logic

ASP.NET Web Pages are ASP.NET sites without models, views, controllers, or project files. Now you may think that’s just like Classic ASP, or PHP?. It’s the full power and speed of the .NET with full syntax of C#. It’s simple, lightweight, fast, super powerful, and I am surprised more people don’t use it.

How to code behind, seperate logic or Object Oriented programming in web pages?

Right click onto your website > Add > New Folder and name is App_Code. This is the folder that will contain all your class files.

 Create a new class file in this folder called “Product.cs”



Copy the following code in this file:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Product
    public int id { get; set; }
    public string desc { get; set; }
    public double price { get; set; }

    public Product()

    //get product by id
    public void getProductByID(int id)
    { = 1;
        this.desc = "ASP.NET Web Pages";
        this.price = 9.99;

This is an example of a class called Product which has 3 properties id, desc and price, a method which would normally get the data from database which we will cover a bit later.

Now create a new Web Page (Razor View cshtml) or in Default.cshtml copy the following

    Product myProduct = new Product();

    catch { <p>Invalid product ID</p>}
    <p>Desc: @myProduct.desc</p>
    <p>Price: @myProduct.price</p>

So this is our Razor view file equivalent of “.asp” or “.php” page which would have html and C# code. The first line creates a new instance of the object Product, we then invoke the method getProductByID which will set the hardcoded values to the object and this is what will be in output. Additionally, just to give you an idea, we are retrieving the ID from Request, try to parse it and provide it to the method.



 Resize images

 public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true)
        int newWidth;
        int newHeight;
        if (preserveAspectRatio)
            int originalWidth = image.Width;
            int originalHeight = image.Height;
            float percentWidth = (float)size.Width / (float)originalWidth;
            float percentHeight = (float)size.Height / (float)originalHeight;
            float percent = percentHeight > percentWidth ? percentHeight : percentWidth;
            newWidth = (int)(originalWidth * percent);
            newHeight = (int)(originalHeight * percent);
            newWidth = size.Width;
            newHeight = size.Height;
        Image newImage = new Bitmap(newWidth, newHeight);
        using (Graphics graphicsHandle = Graphics.FromImage(newImage))
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
        return newImage;



//image from url
Image original = Image.FromFile("/Images/test.jpg");

//do the resize only if the original width is greater than 500
if (original.Width > 500) 
   Image resized = ResizeImage(original, new Size(500, 500));
   MemoryStream memStream = new MemoryStream();
   resized.Save("/Images/test-resized.jpg", ImageFormat.Jpeg);

 More to come…

Leave a Reply

Your email address will not be published. Required fields are marked *