29 August, 2011

Using Multiple Programming Languages in a Asp.net Web Site

  • The App_Code folder does not allow multiple programming languages. 
  • still in a Web site project you can modify your folder structure and configuration settings to support multiple programming languages such as Visual Basic and C#. This allows ASP.NET to create multiple assemblies, one for each language. 
Follow the simple steps to do so :
  1.  create simple website project.
  2.  In the Website menu, click Add ASP.NET Folder, and then click App_Code
  3.  Right-click the App_Code folder and then click New Folder, name the new folder "CSCode".
  4.  In "CSCode" folder, Add new item, choose the Class template, name the class "CSExample", select C# as the language, and click Add.
  5. In Solution Explorer double-click the CSExample.cs file to open it.
  6. Add the following code to the CSExample.cs file, overwriting the existing CSExample code that is already in the file. 
CSExample.cs
public class CSExample
{
    private string teamString;
    public CSExample()
    {
        TeamString = "C# Code";
    }
    public string TeamString 
    {
      get {
        return teamString;
      }
      set {
        teamString = value;
      }
    }
}
  • Create a folder and class for Visual Basic code by repeating steps 3-6 using the following values:
  • New folder: VBCode
  • New class file: VBExample

VBExample.vb
public class CSExample
{
   Public Class VBExample
    Private teamStr As String
    Public Sub New()
        TeamString = "Visual Basic Code"
    End Sub
    Public Property TeamString() As String
        Get
            Return teamStr
        End Get
        Set(ByVal Value As String)
            teamStr = Value
        End Set
    End Property
End Class
}

Now the main part is Modifying the Web.config File 
After creating separate subfolders for each programming language, you must change the Web site project configuration so that ASP.NET will compile the subfolders separately.

To modify the Web.config file to support multiple programming languages

  1. In Solution Explorer, select the name of the Web site project.
  2. If your project does not already have a Web.config file, do the following:
    1. In the Website menu, click Add New Item.
    2. Choose Web Configuration File and then click Add.
  3. Double-click the Web.config file to open it.
  4. Modify the <compilation> section to include a <codeSubDirectories> node by copying the following section and pasting it as a child node of the <compilation> section:

  
  

Testing the classes

CSExample CSCode = new CSExample();
VBExample VBCode = new VBExample();
if (classLabel.Text == CSCode.TeamString.ToString())
{
    classLabel.Text = VBCode.TeamString.ToString();
}
else
{
    classLabel.Text = CSCode.TeamString.ToString();
}

23 August, 2011

Enable Tracing in .net IDE

This is build in service provided by asp.net to map the status of your website.you can use it for debugging purpose. for ex. you can watch all incoming request to your webpage,its status code,rest headers, etc...

There is no extra tool is requred.

you can easily enable it and disable it without editing individual pages. When your application is complete, you can turn off tracing for all pages at once.

2 ways to enable trace 
  1.  page level :   just add "trace" attribute to "true" at @page directive.
  2. Application level : make some setting in web.config as given below.
Example of page level tracing :

<%@ Page language="c#" Trace="true" %>

this will enable tracing option only to particular page.so when you will run this page you can see o/p like image given below after your page information :




 Example of Application level Tracing :

You can enable tracing for an entire application in the Web.config file in the application's root directory. By default, application-level tracing can be viewed only on the local Web server computer. You must set the localOnly attribute to false in the Web.config file to make application-level trace information visible from remote computers.

CAUTION To help keep your Web application secure, use the remote trace capability only when you are developing or deploying your application. Be sure to disable it before you transfer your application onto production Web servers. To disable remote tracing, set the localOnly attribute to true in the Web.config file.

The following example shows an application trace configuration that collects trace information for up to 40 requests and allows browsers on computers other than the origin server to display the trace viewer.

  
     
 

When you enable tracing for an application, ASP.NET collects trace information for each request to the application, up to the maximum number of requests you specify. The default number of requests is 10. When the trace viewer reaches its request limit, the application stops storing trace requests.

Note When you enable tracing for an entire application in the Web.config file, trace information is gathered and processed for each page in that application. To disable tracing for a particular page in the application, set the Trace attribute in that page's @ Page directive to false. Any TraceContext.Write or TraceContext.Warn statements that you include in a page's code are stored and returned to the trace viewer only.

If you want trace information to appear at the end of the page that it is associated with, set the pageOutput attribute in the tracing configuration section of the Web.config file to true. If you want tracing information to be displayed only in the trace viewer, set this attribute to false. If you enable application-level tracing, but you do not want trace information displayed for some pages of the application, use the @ Page directive to set the Trace attribute to false for those pages you do not want trace information displayed in.

Output :


-------------------------------------------------------------------------------------------------------

How to: View ASP.NET Trace Information with the Trace Viewer ?

To view trace details for a specific request

  1. Navigate to Trace.axd in the root of your application.
    For example, if the URL for your application is http://localhost/SampleApplication, navigate to http://localhost/SampleApplication/trace.axd to view the trace information for that application.
  2. Select the View Details link for the request that you want to investigate.

To clear requests from the trace viewer

  1. Navigate to Trace.axd in the root of your application.
  2. Select the clear current trace link to remove all the requests stored in the trace viewer.