ASP.Net Interview Questions - 2

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

  1. Can we make sessions do not use cookies?

    We need to enable "cookieless" property in Web.config.


    <sessionState cookieless="true" timeout="20" />

  2. How can we force all the validation control to run?

    Call Page.Validate

  3. How can we check if all the validation control are valid and proper?

    Using the Page.IsValid() property you can check whether all the validation are done.

  4. If client side validation is enabled in your Web page, does that mean server side code is not run?

    When client side validation is enabled server emit’s JavaScript code for the custom validators. But note that does not mean that server side checks on custom validators do not execute. It does this redundant check two times as some of the validators do not support client side scripting.

  5. Which JavaScript file is referenced for validating the validators at the client side?

    WebUIValidation.js javascript file installed at "aspnet_client" root IIS directory is used to validate the validation controls at the client side. That is true for .Net 1.1. in .Net 2.0 we use axd.file for that. You’ll have to save the AXD file, and then open it in notepad to see the actual javascript.

  6. How to disable client side script in validators?

    Set EnableClientScript property to false.

  7. You find that one of your validation is very complicated and does not fit in any of the validators, what will you do ?

    Best is to go for CustomValidators. Below is a sample code for a custom validator which checks that a textbox should not have zero value.


    <asp:CustomValidator id="CustomValidator1" runat="server"
    ErrorMessage="Number not divisible by Zero"
    ControlToValidate="txtNumber"
    OnServerValidate="ServerValidate"
    ClientValidationFunction="CheckZero" /><br />
    Input:
    <asp:TextBox id="txtNumber" runat="server" />
    <script language="javascript">
    <!--
    function CheckZero(source, args) {
    int val = parseInt(args.Value, 10);
    if (value==0) {
    args.IsValid = false;
    }
    else {
    args.IsValid = true;
    }
    }
    // -->
    </script>

  8. How can I show the entire validation error message in a message box on the client side?

    In ValidationSummary control set property called "ShowMessageBox" to true.

  9. What is Tracing in ASP.NET?

    Tracing allows us to view how the code was executed in detail.

  10. How do we enable tracing?

    On the Application level we can add directive <trace enabled="true" to the web.config between <system.web>…</system.web> directives. On the page level we can add Trace ="true" to the @Page directive in the beginning of the page.

  11. What exactly happens when ASPX page is requested from Browser?

    • The browser sends the request to the webserver. Let us assume that the webserver at the other end is IIS.
    • Once IIS receives the request he looks on which engine can serve this request. When I mean engine means the DLL who can parse this page or compile and send a response back to browser. Which request to map to is decided by file extension of the page requested. Depending on file extension following are some mapping: .aspx for ASP.NET Web pages, .asmx for ASP.NET Web services, .config for ASP.NET configuration files, .ashx for custom ASP.NET HTTP handlers, .rem for remoting resources etc.
    • Once IIS passes the request to ASP.NET engine page has to go through two section HTTP module section and HTTP handler section. Both these section have there own work to be done in order that the page is properly compiled and sent to the IIS.
      HTTP modules inspect the incoming request and depending on that they can change the internal workflow of the request. HTTP handler actually compiles the page and generates output. If you see your machine.config file you will see section of HTTP modules.
    • HTTP handler is where the actual compilation takes place and the output is generated. HTTP handler section is present in WEB.CONFIG file. Depending on the File extension handler decides which Namespace will generate the output. Example all .ASPX extension files will be compiled by System.Web.UI.PageHandlerFactory
    • Once the file is compiled it will be send back again to the HTTP modules and from there to IIS and then to the browser.
  12. How can we kill a user session?

    We can use Session.Abandon directive.

  13. How do you upload a file in ASP.NET?

    The code snippet given below enables you to upload a file to a folder named temp on your server:


    string strdir = "D:\\temp\\";
    string strfilename = Path.GetFileName( txtFile.PostedFile.
    FileName);
    txtFile.PostedFile.SaveAs(strdir+strfilename);

    Make sure to create the specified folder and change the path before attempting to execute the program. In the above code, you can the HTML File control so that users can browse for the required file. As you may know, an HTML control can be converted into an ASP.NET server control with the addition of the runat = "server" attribute. The system retrieves and saves the file using the PostedFile property. Because the HTML file control is used, you have to specifically give the enctype attribute of the Form tag:


    <form method = "post" name = "frmemail" runat = "server"
    enctype = "multipart/form-data"
    onSubmit = "return Tocheck(this)">

    Visual Studio 2005 ships with a built-in control named "FileUpload". Hence, the usage of the HTML File control can be avoided. Also, there is no need to give the enctype attribute as shown above. The new control automatically handles the encryption.

  14. How do I send email message from ASP.NET ?

    To send an email from your ASP.NET page, you need to:

    • import the System.Web.Mail namespace in your ASP.NET page.
    • create an instance of the MailMessage class
    • set all the properties of the MailMessage instance.
    • send the message with SmtpMail.Send method.
  15. What are different IIS isolation levels?

    IIS has three level of isolation:

    • Low (IIS process) In this main IIS process and ASP.NET application run in same process. So if any one crashes the other is also affected. So all application and the IIS process runs on the same process. In case any website crashes it affects everyone.
    • Medium (Pooled) In Medium pooled scenario the IIS and web application run in different processes. So in this case there are two processes process1 and process2. In process1 the IIS process is running and in process2 we have all Web applications running.
    • High (Isolated) In high isolated scenario every process is running is there own process. This consumes heavy memory but has highest reliability.
  16. ASP used STA threading model, what is the threading model used for ASP.NET ?

    ASP.NET uses MTA threading model.

  17. What is the use of <%@ page aspcompat=true %> attribute?

    This attribute works like a compatibility option. As mentioned before ASP worked in STA model and ASP.NET works in MTA model, but what if your ASP.NET application is using a VB COM component. In order that VB COM runs properly in ASP.NET threading model we have to set attribute. After defining the ASPCOMPAT directive attribute ASP.NET pages runs in STA model thus building the compatibility between ASP.NET and old COM components that does not support MTA model.

  18. If cookies are not enabled at browser end does form Authentication work?

    No, it does not work.

  19. What is the difference between "Web farms" and "Web garden"?

    "Web farms" are used to have some redundancy to minimize failures. It consists of two or more web server of the same configuration and they stream the same kind of contents. When any request comes there is switching / routing logic which decides which web server from the farm handles the request. For instance we have two servers "Server1" and "Server2" which have the same configuration and content. So there is a special switch which stands in between these two servers and the users and routes the request accordingly.

    A router in between which takes a request and sees which one of the server is least loaded and forwards the request to that server. So for request1 it route’s server1, for request2 it routes server2, for request3 it routes to server3 and final request4 is routed to server4. So you can see because we have web farm at place server1 and server2 are loaded with two request each rather than one server loading to full. One more advantage of using this kind of architecture is if one of the servers goes down we can still run with the other server thus having 24×7 uptime.

    The routing logic can be a number of different options:

    • Round-robin: Each node gets a request sent to it "in turn". So, server1 gets a request, then server2 again, then server1, then server2 again.
    • Least Active Whichever node show to have the lowest number of current connects gets new connects sent to it. This is good to help keep the load balanced between the server nodes
    • Fastest Reply Whichever node replies faster is the one that gets new requests. This is also a good option - especially if there are nodes that might not be "equal" in performance. If one performs better than the other, then send more requests there rather than which is moving slowly?

    Web Garden

    All requests to IIS are routed to "aspnet_wp.exe" for IIS 5.0 and "w3wp.exe" for IIS 6.0. In normal case i.e. with out web garden we have one worker process instance ("aspnet_wp.exe" / "w3wp.exe") across all requests. This one instance of worker process uses the CPU processor as directed by the operating system.

    But when we enable web garden for a web server it creates different instances of the worker process and each of these worker process runs on different CPU. You can see in the below diagram we have different worker process instances created which run on different CPU’s.

    In short we can define a model in which multiple processes run on multiple CPUs in a single server machine are known as a Web garden.

  20. How do we configure "WebGarden"?

    "Web garden" can be configured by using process model settings in "machine.config" or "Web.config" file. The configuration section is named <processModel> and is shown in the following example. The process model is enabled by default enable="true"). Below is the snippet from config file.


    <processModel
    enable="true"
    timeout="infinite"
    idleTimeout="infinite"
    shutdownTimeout="0:00:05"
    requestLimit="infinite"
    requestQueueLimit="5000"
    memoryLimit="80"
    webGarden="false"
    cpuMask="12"
    userName=""
    password=""
    logLevel="errors"
    clientConnectedCheck=”0:00:05"
    />

    From the above processModel section for web garden we are concerned with only two attributes "webgarden" and "cpuMask".

    webGarden : Controls CPU affinity. True indicates that processes should be affinitized to the corresponding CPU. The default is False.

    cpuMask : Specifies which processors on a multiprocessor server are eligible to run ASP.NET processes. The cpuMask value specifies a bit pattern that indicates the CPUs eligible to run ASP.NET threads. ASP.NET launches one worker process for each eligible CPU. If webGarden is set to false, cpuMask is ignored and only one worker process will run regardless of the number of processors in the machine. If webGarden is set to true, ASP.NET launches one worker process for each CPU that corresponds to a set bit in cpuMask. The default value of cpuMask is 0xffffffff.

    Use 1 for the processor that you want to use for ASP.NET. Use 0 for the processor that you do not want to use for ASP.NET. For example, if you want to use the first two processors for ASP.NET of a four-processor computer, type 1100.

  21. What is the main difference between Gridlayout and FlowLayout ?

    GridLayout provides absolute positioning for controls placed on the page. Developers that have their roots in rich-client development environments like Visual Basic will find it easier to develop their pages using absolute positioning, because they can place items exactly where they want them. On the other hand, FlowLayout positions items down the page like traditional HTML. Experienced Web developers favor this approach because it results in pages that are compatible with a wider range of browsers.

    If you look in to the HTML code created by absolute positioning you can notice lot of DIV tags. While in Flow layout you can see more of using HTML table to position elements which is compatible with wide range of browsers.

Comments

Leave a Reply

You must be logged in to post a comment.