Common ASP.NET Tips and Tricks - Part 3

In previous series of ASP.NET Tips and Tricks we learned basic configuration for ASP.Net applications and exception handling. You can also visit Part 1 and Part 2. In this article we learn new tips that are useful for both web forms and ASP.Net MVC applications.

Description

  1. When we are using the Response.Redirect method inside a Try Catch block a ThreadAbortException occurs.

    Solution 1
    1. Response.Redirect(“redirectUrl”,false);  
    This method ends the current execution of the request and the error is prevented.

    Solution 2

    The second way to prevent a ThreadAbortException error is to handle the error in a try catch block.
    1. try {  
    2.    Response.Redirect(“redirectToSomeUrl”);  
    3. catch(ThreadAbortException e) {  
    4. }  
  2. The server cannot set the status after HTTP headers have been sent.

    Solution: This error occurs for a number of reasons in ASP.Net MVC applications
    When we export to Excel or any other format of export this type of error occurs. To prevent this type of error use the return type new EmptyResult() for ActionResult.

  3. Various types of ActionResult in ASP.Net MVC.

    Answer:

    ViewResult: Renders a view as a HTML page.

    PartialViewResult: This is a reusable component. It renders a small person of the view in the page.

    RedirectResult: Redirect to another action method.

    RedirectToRouteResult: Redirect to another action method. A route parameter is required, for example 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 the client.

    FileResult: Returns a binary output to write to the response.

    EmptyResult: Returns a null result.

  4. What is the difference between a string.Empty and “”?

    Answer: string.Empty is a runtime constant. Its value is evaluated at runtime.

    We cannot pass a string.Empty as the default argument in any method as the parameter because a default parameter in methods require a compile-time constant.
    1. public static void SomeMethod(string p1=string.Empty)  
    2. {  
    3.    //To Do Write some code  
    4. }  
    This method does not compile successfully:
    1. public static void SomeMethod(string p1=””)  
    2. {  
    3.    //To Do Write some code  
    4. }   
    Compilation failed because string.Empty is a runtime constant.

  5. Unfamiliar behavior of string.Trim() function

    Solution: When we use the trim function for a null value a NullRefrence Exception occurs so the best way to prevent this error is to always check for a null and then use the Trim function.

    To check the online behavior of the Trim function visit IdeOne.
    1. public static void Main()  
    2. {  
    3.     // your code goes here  
    4.     string s1=null;  
    5.     string s2=string.Empty;  
    6.       
    7.     Console.WriteLine(s2.Trim());  
    8.     Console.WriteLine(s2.Trim().Length);  
    9.         //Below code returns runtime error  
    10.         //  Console.WriteLine(s1.Trim());  
    11.        //   Console.WriteLine(Convert.ToString(s1).Trim());  

Conclusion

In this article we learned the basis tips and tricks for ASP.NET MVC and common errors.


Similar Articles