Handlers in ASP.NET

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags with 2 Comments on Handlers in ASP.NET

HTTP Handlers is a new technique presented in ASP.NET that was not present in the “Classic ASP”. HTTP Handlers are components that implement the System.Web.IHttpHandler interface. Unlike ASP.NET pages Handlers dont have HTML-markup file, no events and other supporting. All they have is a code-file (written in any .NET-compatible language) that writes some data to the server HTTP response.

ASP.NET handlers have “.ashx” file extension (unlike pages, that have “.aspx” file extension).

Handlers are considered to be more lightweight object than pages. That’s why they are used to serve dynamically-generated images, on-the-fly generated PDF-files and similar content to the web-browser.

One such scenario which may arise in any product is that if  I owe a site and I want to restrict the user from downloading a file if he/she is not logged in. But if the user (not logged) is allowed to see the url of the downloading file then he can just copy the url and paste it in another tab and he can easily download the file from there, i.e. my protection mechanism failed. Now here comes the use of handlers.

The handlers will restrict the user to even view  the url of the file to be downloaded unless he is logged in.

How to create the Handlers in ASP.NET in Visual Studio 8.0.

First step is to create new project-

Step 1) Open Visual Studio an Goto – File Menu ->New -> Project.

Step 1
Step 1

Step 2) Now new window will open then select  Web -> ASP.NET Web Appliction. Then rename it and click ok.

Step 2
Step 2


Step 3) Now we have to add the Handler (Don’t call it the page, its all different thing) Follow the steps to include the handler in the current project.

First Right Click on the Solution Explorer -> Add -> New Item ->

Step 3

Step 4) Then new window will be displayed. Then select the Generic Handler and rename it then click on Add–>

 

ASP.Net Handlers
Step 4

Step 5) Now open the Default.asp page in Design view and design the form as required using Button Tool from toolbox.

Step 5
Step 5

Step 6) Then write the following code on the click event of both the buttons in Default.aspx.cs page (Just double click on the buttons and it will automatically generate the block in source page)

protected void btnWoutSecurity0_Click(object sender, EventArgs e)
{
     Response.Redirect("LinkSecurty.ashx?isSecure = 1");
}
protected void btnWoutSecurity_Click(object sender, EventArgs e)
{
      Response.Redirect("LinkSecurty.ashx");
}
Step 7) Now open the Handler which we have created earlier i.e. LinkSecurity.aspx.cs  and write the following code–
public class LinkSecurity : IHttpHandler
{
     public void ProcessRequest(HttpContext context)
     {
        if (context.Request["isSecure"] == null)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("You are not Logged In.... Please Login to access this page...");
        }else
        {
            context.Response.ContentType = "image/png";
            context.Response.WriteFile("imageName.png")
        }
      }
      public bool IsReusable
      {   get{return false;}
      }
}

Upload Files in ASP.NET at Production server

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , , , with Leave a comment on Upload Files in ASP.NET at Production server

In this article, i am going to demonstrate that how to upload the file in ASP.NET production server.

Most of the case, a developer created a code to upload the file and test it on his local machine. program runs smoothly, but as he forward the same code on production. He stuck in the file permission error as on the local he has the full permission whereas on development its not the case.

So i decided to write this tutorial for the beginners which will demonstrate lots of ASP.NET features.

First we need to create a folder in Server and share it with ASP.NET web server as described.

Right click on folder, select Web sharing tab and select the radio button “Share this folder”. One  window appear, give it alias name which will be used in coding.

After this, open Run box and type “inetmgr“. you will see that your web shared folder appears in the IIS.

Right click on folders property. one window will opened.

Select “Directory Security” tab then click on “Edit” button and then check “Anonymous access”. And because of this setting any body can access or download that file.

Now we are ready, to create a sample application which will demonstrate that how to upload the file and use it.

Create ASP.NET project in Visual Studio. Create a webpage with fileupload, hyperlink and button control.

<form id="form1" runat="server">
<asp:FileUpload ID="uploadFile" runat="server" /><br />
<asp:HyperLink ID="hypLink" runat="server" Visible="false" Target="_blank"></asp:HyperLink><br />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
</form>

In web.config add two keys.  One for actual folder location in which we are going to upload the files and other its alias name. we can also hard code that, but its good practice to make it configurable at any time because in near future we may want to change the location.


<appSettings>

<add key="DemoAttachment" value="D:ShareDemoShare"></add>

<add key="DemoAttachmentVirtualPath" value="http://localhost/DemoShare/"></add>

</appSettings>

In DemoAttachmentVirtualPath key, value should like “http://machinename/” + Alias folder name.

On click event of button, write below code:

string strPath = ConfigurationManager.AppSettings["DemoAttachment"];
uploadFile.SaveAs(strPath + System.IO.Path.GetFileName(uploadFile.FileName));
hypLink.Visible = true;
string VirtualPath = ConfigurationManager.AppSettings["DemoAttachmentVirtualPath"];
hypLink.Text = System.IO.Path.GetFileName(uploadFile.FileName);
hypLink.NavigateUrl = VirtualPath + uploadFile.FileName;
string strPath = ConfigurationManager.AppSettings["DemoAttachment"];

uploadFile.SaveAs(strPath + System.IO.Path.GetFileName(uploadFile.FileName));
hypLink.Visible = true;
string VirtualPath = ConfigurationManager.AppSettings["DemoAttachmentVirtualPath"];

hypLink.Text = System.IO.Path.GetFileName(uploadFile.FileName);

hypLink.NavigateUrl = VirtualPath + uploadFile.FileName;

The output will look like:

Download Source code

For the file size limitation, please visit this article : http://JitendraZaa.com/blog/?p=125

cannot access a closed file – FileUpload in ASP.NET

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , with 14 Comments on cannot access a closed file – FileUpload in ASP.NET

This was very interesting error, i got during development of file upload control in ASP.NET.

On my local system, every thing was just fine. but when i deployed my application on development server, my control was able to upload only small size files.

whenever i tried to upload large size file i got error “cannot access a closed file”

After going through MSDN i found that there is one new element added in ASP.NET 2.0 known as httpRuntime.

So i just added this new Tag in my web.config and every thing was right.

<system.web>
<httpRuntime  maxRequestLength="15360" requestLengthDiskThreshold="15360"/>
</system.web>

Note :

requestLengthDiskThreshold should not exceed the maxRequestLengthattribute.

To read more about this, please visit http://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

XML Parsing Error: no element found

Author posted by Jitendra on Posted on under category Categories ASP.NET, Tech Tips and tagged as Tags , with 3 Comments on XML Parsing Error: no element found

Few Days ago, i found strange error like “XML Parsing Error: no element found“.

I was unable to find the exact root cause. Most of the times it happens if you are missing any closing tag in your ASPZ application. I checked my complete page and reconfirmed but error still persist.

somehow i found that ASP.NET treat the response of page as XML document and that’s why we receive XML Parsing Error: no element found error.

so explicitly i set the response type of my page as text/HTML. Response.ContentType = “text/HTML”.

And this line tells the ASPX that page is HTML, not XML.

WordPress Error: “missing A Temporary Folder.” After Uploading A File

Recently i got the error

“missing A Temporary Folder.” After Uploading A File

After digging lots of sites, i was unable to fix the problem.

Maximum sites offer solution like change php.ini etc. but changing configuration file and using shell command can be avoided by a simple trick.

Go to Plugins Tab in wordpress after Login.

Then click on Install, you will get below Screen.

click on Add New and search FCKEditor, Install It -> Activate.


And thus your problem solved.

you want to use that editor then use or Simply Deactivate but don’t uninstall.


Display author name in wordpress

Author posted by Jitendra on Posted on under category Categories Tech Tips and tagged as Tags , with 1 Comment on Display author name in wordpress

Look in the default theme’s index.php file for the proper template tag:


<code><?php the_author() ?></code>

and a good place to put it.

Display Author’s ‘Public’ Name

Displays the value in the user’s Display name publicly as field.

<p>This post was written by <?php the_author(); ?></p>

Get Author link


<p>Written by:
<?php echo get_the_author_link(); ?></p>

The Author’s Post

<p><?php the_author(); ?> has blogged <?php the_author_posts(); ?></pre>
</div>
<pre>posts</p>

The Author’s Post Link

<p>Other posts by <?php the_author_posts_link(); ?></p>

for more info check below URL
http://codex.wordpress.org/Template_Tags/the_author

Install the OS on CD/DVD/Flash/Pen Drive

Author posted by Jitendra on Posted on under category Categories Tech Tips and tagged as Tags with 3 Comments on Install the OS on CD/DVD/Flash/Pen Drive

Pocket PC..

This article will show you how to install Windows XP on your CD/DVD/USB Pen drive/ Flash Drive/ thumb Drive.

Procedure:-

1) Make sure that your system ( to be booted ) has about 500MB of free disk space!

2) Download the latest PE Builder version (self-installing package) and install it. Follow me to Download.
3) Start PE Builder (pebuilder.exe). When you start PE Builder for the first time it will ask if you agree with the license agreement.

4) Now PE Builder will ask to search for windows installation files. If you don’t have your windows XP setup/installation files on your system you must insert the original Microsoft WIN XP installation/setup CD . Click “yes” to start searching. PE Builder will now search all fixed- and CD-Rom drives for Windows installation files. This will take some time. When more than one valid location is found, a dialog will appear where you can select which location you want to use.

5) For making Bootable CD/DVD :

At the main PE Builder dialog, select the “Burn to CD/DVD” option. When you are using an erasable medium, make sure that the “AutoErase RW” option is enabled. The “burn using” option should be set to “StarBurn”. Select your CD writer device from the Device list.

Click on  “build” button. PE Builder will now ask you to create a BartPE directory, answer with “yes”.

The license for your Microsoft Windows XP product is shown. Read it and agree to it to continue.

PE Builder will now start building BartPE. This will take a few minutes.
You will see a lot of files getting copied and/or decompressed, the ISO image build and the data recorded to your CD/DVD writer.
If the data verify was correct and there where no errors reported you can boot the CD/DVD!

6)For making bootable Pen Drive:

Follow the above instructions and copy the files to the pen drive.

Enjoy your new preinstalled XP environment and use it for system Rescue Purposes…

Install OS From Your Flash/Pen Drive.

Author posted by Jitendra on Posted on under category Categories Tech Tips with Leave a comment on Install OS From Your Flash/Pen Drive.

Simple way to take your Desktop PC into your pocket via USB.

Most PC users have got very much habituated in installing Windows Operating system on their computer using the Windows Installation CD’s, but what if a user does not have a CD-ROM to read them and if he has a option of only using a Pen Drive? Then here is a small tutorial on how to get started and install a complete Operating System using the Pen Drive as Booting Medium and a drive to Store the Setup files.

First of all you need to make sure that you have got a Pen Drive with a total capacity of minimum 256Mb for the storage of the Setup Files though you can opt for the higher capacity drives and you need to make sure that the format of the Disk is correctly set before the installation process. Hence here we are going to use a tool  HP USB Disk Storage Format Tool for Formatting a USB Drive into the right File System which can be downloaded (Open Source Software- HP USB Disk Storage Format Tool – v2.1.8 ). Since most BIOS wont accept any drive and would only accept FAT File System you need to make sure that you have formatted the USB Flash Drive using this tool.

Follow this link To download the USB Format Software

Boot menu may vary.
Modal Boot Page

Next you need to get ready loading the OS files into the Flash Drive, following which you need to setup the Boot Device in the Advanced BIOS Features. The new version MotherBoards would have a option called “˜USB-HDD’ before the Hard Drive option and if you dont find this available you need to update your BIOS. If you find it, you need to change the order of booting and make sure that the USB-HDD is in the first option. Next save the BIOS settings and restart the computer.

There can be other names also listed for these Pen Drives which can be likely Removable Device, Removable Storage Device etc whom you need to select and make sure they are in the first position in the Boot Device Priority Section as that we do during booting xp from CD drive..

Regards–

Nikhil Naoghare.

Windows 7 theme in XP

Author posted by Jitendra on Posted on under category Categories Tech Tips with Leave a comment on Windows 7 theme in XP

First,  download some of the themes you like to your Windows XP or Vista desktop.

dowload windows 7 themes

These files have a unique .themepack extension which is just another zip format containing all of the elements of a theme including the background images, Aero glass colors, sounds, cursors, icons, screen savers, etc.

To use these files on an older version of Windows, you first need to extract them as only Windows 7 can natively understand the themepack format. You can either change the file extension of the Windows 7 theme file from .themepack to .cab and then extract the files with a right-click, or install the free 7 zip  utility as 7-Zip can automatically hand the themepack file format.

windows 7 themepack file Extract themepack with 7-Zip Rename to CAB and extract in Explorer

Once extracted, your theme files will now be in a new folder that has the same name as your original themepack.

Most Windows 7 themes consist of multiple background images and Windows 7 will automatically change your desktop background at periodic intervals using one of the images from the themepack. Windows Vista and XP do not support desktop slideshows, but you can easily bring that feature use the excellent and free John’s Background Switcher program.

To create a desktop slideshow, click the add button and choose “A folder on your computer.” Now select the Windows 7 theme folder that contains all the background images. The program allows lot of tweaking but you can skip that because the default settings are very similar to what you need for simulating Windows 7 behavior on your XP or Vista computer.

Add folder containing Windows 7 wallpapers Choose your Windows 7 Theme Folder

However, John’s Background Switcher by default shows the name of the image in the top right hand corner of the picture.  To remove this, click the “More…” button that has a gear icon on it.  Uncheck the box beside the 3rd entry which says “Show picture info on the top-right corner of the screen.”  Now click Ok, and then click Ok in the main window, and your desktop slideshow is all ready.

ClearType in IE

Author posted by Jitendra on Posted on under category Categories HTML and tagged as Tags , , with Leave a comment on ClearType in IE

Recently, during development i encountered in interesting issue related to internet explorer.

There is one property in IE known as cleartype, which results in different display of font.

you can see in below snap :

clear type font style in IE
clear type font style in IE

The Browse button at left side has shadow effect in font or we can say Cleart type is enable for that control.

the control at right side is normal Button control of HTML. you can see difference very clearly.

Remove ClearType property from IE:

To remove clearType property, we just need below trick in CSS.


<style type="text/css">
body {
filter:none
}
</style>

By using property filter:none we can remove the cleart type behavior of IE.

Text without clear type
Text without clear type