Tuesday, May 31, 2011

How to avoid the error "Maximum Request Length Exceeded"

Scenario :


I had a requirement to develop a page which has to upload the file to the DB server. Uploading a file to the server is a very easy thing with the FileUpload control. By default the maximum size allowed to upload is 4MB and if you want you can increase or decrease in web.config settings.

Problem :

There won't be any problem, if you upload the file size of less than the specified size in web.config. If the size of the file is more than the limit specified, the runtime will be thrown with the message of "Maximum Request Length Exceeded". The thing is it won't be catched in button click of the File upload. Let us see how can we fix this. I found the solution in web and explaining for you.

Solution:  

I'm not sure this is the best way to do it. But this also a solution. We could catch this in Application_BeginRequest event of Global.asax. Normally whatever the client request for a page or file this application BeginRequest event will be fired and the control goes to the page events. Since its going for all the postback and initial load. We can calculate the content length of each post by the below lines,

            HttpContext context = ((HttpApplication)sender).Context;
            int PageContentLength = context.Request.ContentLength;

In addition with that, we need to calculate the actual size of the page and do some calculations. Look at the entire code below, which have to be added in Global.asax.

Event in Global.asax


The last line redirects the control to the same page with the url querystring. Based on the query string, we'll display the error message in Attachment page. Another thing here is as soon as you displayed the error message you need to remove the querystring value.

Conclusion:


This solution may not be better for performance wise since this event is keep calling for all the page requests as well file requests like .JS or .CSS. Let me know if you have better solution than this.

Happy Coding....




Thursday, May 26, 2011

Find from which page the master file is invoked

Scenario

We may sometimes need to know from which page, the master file is invoked. Based on the file, we'll do some operation not for the other pages. For example if the user name in session is empty, it has to redirect to the login page otherwise the requested page should be opened. But at the same time, if the user request a login page, it should not redirect to the same page again.

Solution


In the above said scenario, we have to identify from where the master page is being called. We have the below code to identify from which page its called,

this.Parent.TemplateControl.AppRelativeVirtualPath.ToString() 


If you put this code in master page file, you will get to know the page from where it's invoked. Its taking the parents relativevirtualpath.


Happy Coding...



Wednesday, May 25, 2011

Change the upload file size in ASP.NET application

Problem:


This is a scenario i faced when i worked in web application. Our application supports to upload the documents. When the user tries to upload the file more than 4MB, the browser shows error message and . Initially I did the work around, check the size of the file and notify the user to upload less than 4 MB document.

Solution

I found that thats not the correct solution. We may stop breaking the application and google the web and found a very simple solution to solve the problem.

We need to add the below line in web.config file under System.web tag
<httpRuntime maxRequestLength="5120"/>

The httpRuntime tag has the property called maxRequestLength, which is to be specified in KB. The above shows the 5 MB file can be uploaded.

If you are not specifying the httpRuntime in web.config..the default size is 4MB.

Conclusion:


With that above work around, user can able to upload more than 4 MB and we can fix the size.

Happy Coding... :)