Common Asp.Net Tips and Tricks Part -2

Introduction
In previous series of Asp.Net Tips and Tricks we learn basic configuration for Asp.Net application and exception handling. You can also visit Part-1  In this article we learn new tips which is useful for both web form and Asp.Net MVC application.

Description

In this article we learn some tips which will helpful while we development.
Problem: When we are using Response.Redirect method inside Try Catch Block ThreadAbortException occurs
Solution 1: Response.Redirect(“redirectUrl”,false);

This method stops current execution of request and error is prevented.

Solution 2: Second way to prevent ThreadAbortException error is to handle error in try catch block
    try {
       Response.Redirect(“redirectToSomeUrl”);
     } catch(ThreadAbortException e) 
     {
     }

Problem:Server cannot set status after HTTP headers have been sent

Solution: This error occurs with number of reason in Asp.Net MVC application
When we use export to excel or any other format of export this type of error occurs to prevent this type of error use return type new EmptyResult() for ActionResult.

Problem:Different type of ActionResult  in Asp.Net MVC
Solution:
ViewResult: Renders a view as Html page

PartialViewResult: This is reusable component. Renders a small person of view in Page

RedirectResult: Redirect to another action method

RedirectToRouteResult: Redirect to another action method. Route parameter are required e.g. Action Name and Controller Name

ContentResult: Returns user defined content types

JsonResult: Returns a serialize JSON object

JavaScriptResult: Returns a script that can be executed to client

FileResult: Returns a binary output to write to the response

EmptyResult: Returns a null result

Problem: What is difference between string.Empty and  “”
Solution : string.Empty is runtime constant. Its value is evaluated runtime.
We cannot pass string.Empty as default argument  in any method as parameter because default parameter in Methods required compile time constant


public  static void  SomeMethod(string p1=string.Empty){
//To Do Write some code }
This method not complied success fully
public  static void SomeMethod(string p1=””)
{//To Do Write some code
}
Compilation failed because string.Empty is runtime constant
Problem: Unfamiliar behavior of  string.Trim() function
Solution:
When we use trim function for null value NullRefrence Exception occurs so best way to prevent this error is to always check null safe and then use Trim function.
To check online behavior of Trim function visit IdeOne

public static void Main() {
// your code goes here
string s1=null;
string s2=string.Empty;
Console.WriteLine(s2.Trim());
Console.WriteLine(s2.Trim().Length);
//Below code returns runtime error
//    Console.WriteLine(s1.Trim());
//    Console.WriteLine(Convert.ToString(s1).Trim());
}

Conclusion

In this article we learn basis tips and tricks for asp.net MVC and Common errors.

Comments

Popular posts from this blog

Dynamic Query in LINQ using Predicate Builder

Common Asp.Net Tips and Tricks

Payumoney Integration With Asp.Net