Solving development problems  |  About this blog

Archive for the ‘ASP.NET’ tag

Parsing URL query string into key-value pairs in C#

Imagine you have URL something like this:

http://www.yourdomain.com?param1=value1&param2=value2&param3=value3&param4=value4

and you want to get key-value pairs such as

  • {“param1″, “param2″, “param3″, “param4″}
  • {“value1″, “value2″, “value3″, “value4″}

It is easy to do using system function:

using System.Collections.Specialized;
NameValueCollection query = HttpUtility.ParseQueryString(queryString);
Response.Write(query["param1"]);

Written by Avivo

December 3rd, 2010 at 1:08 pm

System.Web.HttpException: Request is not available in this context

Use HttpContext.Current.Request instead of Request

i.e.

HttpContext.Current.Request.UserHostAddress

not

Request.UserHostAddress

Comparison: ASP .NET WebForms vs MVC

Comparison: ASP .NET WebForms vs MVC

WebForms MVC
  • Requires .NET 2.0
  • Event model – button click triggers an event in code-behind
  • ViewState – server-based forms for easier management
  • Existing third party controls
  • Requires .NET 3.5 and MVC library
  • Requires IIS 6.0 or later for URL rewritting – btw, max IIS version for WinXP is 5.1
  • Backward compatible – developer can include WebForms pages into MVC website and vice-versa
  • Dividing an application into the model, the view, and the controller – data, design and code are separated
  • For large teams of developers and designers (parallel work)
  • No ViewState means smaller output and cleaner html
  • Portable among other languages (like php) – similar file hierarchy, object names, different syntax
  • 30-50% faster than Web Forms, 100-800 requests per second
  • Native URL rewritting
  • Skinnable, suitable for websites that are viewed in computer browsers and mobile browsers
  • Unit testing
  • Written by Avivo

    January 30th, 2010 at 11:41 pm

    IF WRN: Assembly binding logging is turned OFF or It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level

    We had some problems with subdirectories under our root website.

    We wanted to add different examples under our website root (organized into folders) like this:

    • www.our-website.com/example1
    • www.our-website.com/example2

    And we always got these errors.

    At root of our website www.our-website.com we didn’t have any web.config and we didn’t want any. Every subfolder has its own web.config and it didn’t worked under IIS7

    Solution:

    Click on each folder under IIS and then right mouse click Convert To Application and it worked.

    Saving high quality JPG in C# or ASP.NET

    Just use this piece of code or read at this MSDN article:

    private void VaryQualityLevel()
    {
    	// Get a bitmap.
    	Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    	ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
    
    	// Create an Encoder object based on the GUID
    	// for the Quality parameter category.
    	System.Drawing.Imaging.Encoder myEncoder =
    	System.Drawing.Imaging.Encoder.Quality;
    
    	// Create an EncoderParameters object.
    	// An EncoderParameters object has an array of EncoderParameter
    	// objects. In this case, there is only one
    	// EncoderParameter object in the array.
    	EncoderParameters myEncoderParameters = new EncoderParameters(1);
    
    	EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
    	myEncoderParameters.Param[0] = myEncoderParameter;
    	bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);
    
    	myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    	myEncoderParameters.Param[0] = myEncoderParameter;
    	bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);
    
    	// Save the bitmap as a JPG file with zero quality level compression.
    	myEncoderParameter = new EncoderParameter(myEncoder, 0L);
    	myEncoderParameters.Param[0] = myEncoderParameter;
    	bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);
    
    }
    
    ...
    
    private ImageCodecInfo GetEncoder(ImageFormat format)
    {
    	ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    
    	foreach (ImageCodecInfo codec in codecs)
    	{
    	if (codec.FormatID == format.Guid)
    	{
    	return codec;
    	}
    	}
    	return null;
    }

    Written by Avivo

    November 13th, 2009 at 4:17 pm