Friday, October 26, 2012

restrict download


The null coalescing operator: ??



This is a new feature of c# 2.0. The null coalescing operator is a short cut for checking if a value is null and if so returning the value of the second operand. Kind of like an IIF. The syntax is as follows
string newValue = someValue ?? "default";
The first operand someValue must be a nullable type. The above code will set the value of newValue to someValueunless it's null, then it'll set it to "default".
You could also use this inline:

Console.WriteLine("The value is " + (someValue ?? "null"));


another use
return (bool)(ViewState["IsPaged"] ?? true);


If you really want to use an identifier that clashes with a keyword, you can qualify it with the @ prefix. For instance:
 class class  {...}    // illegal
 class @class {...}    // legal
The @ symbol doesn't form part of the identifier itself, so @myVariable is the same as myVariable. check out this example
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication7
{
   class Program
   {
       static void Main(string[] args)
       {
           int @int = 10;
           double @double = 10.121;
           float @float = (float)(@int + @double);
           Console.WriteLine(@float.ToString());
           Console.Read();

       }
   }
}

How to restrict download of specified file types in asp.net

In this post i will show you how to restrict your web app so that .txt (whatever file extensions you want ) files can not be downloaded. Open your web.config file and register following setting in your web.config
<system.web>
   <httpHandlers>
       <add verb="*" path="*.txt" type="System.Web.HttpForbiddenHandler" />
   </httpHandlers>
</system.web>
For more details how to register handler in web.config check out this link