You are on page 1of 16

ASP.

NET Application Life Cycle


The application life cycle has the following stages:

 User makes a request for accessing application resource, a page. Browser sends
this request to the web server.

 A unified pipeline receives the first request and the following events take place:

o An object of the class ApplicationManager is created.

o An object of the class HostingEnvironment is created to provide


information regarding the resources.

o Top level items in the application are compiled.

 Response objects are created. The application objects such as HttpContext,


HttpRequest and HttpResponse are created and initialized.

 An instance of the HttpApplication object is created and assigned to the request.

 The request is processed by the HttpApplication class. Different events are


raised by this class for processing the request.

ASP.NET Page Life Cycle


Following are the different stages of an ASP.NET page:

 Page request - When ASP.NET gets a page request, it decides whether to parse
and compile the page, or there would be a cached version of the page;
accordingly the response is sent.

 Starting of page life cycle - At this stage, the Request and Response objects
are set. If the request is an old request or post back, the IsPostBack property of
the page is set to true. The UICulture property of the page is also set.

 Page initialization - At this stage, the controls on the page are assigned
unique ID by setting the UniqueID property and the themes are applied. For a
new request, postback data is loaded and the control properties are restored to
the view-state values.
 Page load - At this stage, control properties are set using the view state and
control state values.

 Validation - Validate method of the validation control is called and on its


successful execution, the IsValid property of the page is set to true.

 Postback event handling - If the request is a postback (old request), the


related event handler is invoked.

 Page rendering - At this stage, view state for the page and all controls are
saved. The page calls the Render method for each control and the output of
rendering is written to the OutputStream class of the Response property of
page.

 Unload - The rendered page is sent to the client and page properties, such as
Response and Request, are unloaded and all cleanup done.

ASP.NET Page Life Cycle Events


At each stage of the page life cycle, the page raises some events, which
could be coded. An event handler is basically a function or subroutine,
bound to the event, using declarative attributes such as Onclick or handle.

Following are the page life cycle events:

 PreInit - PreInit is the first event in page life cycle. It checks the IsPostBack
property and determines whether the page is a postback. It sets the themes
and master pages, creates dynamic controls, and gets and sets profile property
values. This event can be handled by overloading the OnPreInit method or
creating a Page_PreInit handler.

 Init - Init event initializes the control property and the control tree is built. This
event can be handled by overloading the OnInit method or creating a Page_Init
handler.

 InitComplete - InitComplete event allows tracking of view state. All the


controls turn on view-state tracking.

 LoadViewState - LoadViewState event allows loading view state information


into the controls.
 LoadPostData - During this phase, the contents of all the input fields are
defined with the <form> tag are processed.

 PreLoad - PreLoad occurs before the post back data is loaded in the controls.
This event can be handled by overloading the OnPreLoad method or creating a
Page_PreLoad handler.

 Load - The Load event is raised for the page first and then recursively for all
child controls. The controls in the control tree are created. This event can be
handled by overloading the OnLoad method or creating a Page_Load handler.

 LoadComplete - The loading process is completed, control event handlers are


run, and page validation takes place. This event can be handled by overloading
the OnLoadComplete method or creating a Page_LoadComplete handler

 PreRender - The PreRender event occurs just before the output is rendered. By
handling this event, pages and controls can perform any updates before the
output is rendered.

 PreRenderComplete - As the PreRender event is recursively fired for all child


controls, this event ensures the completion of the pre-rendering phase.

 SaveStateComplete - State of control on the page is saved. Personalization,


control state and view state information is saved. The HTML markup is
generated. This stage can be handled by overriding the Render method or
creating a Page_Render handler.

 UnLoad - The UnLoad phase is the last phase of the page life cycle. It raises the
UnLoad event for all controls recursively and lastly for the page itself. Final
cleanup is done and all resources and references, such as database connections,
are freed. This event can be handled by modifying the OnUnLoad method or
creating a Page_UnLoad handler.

Difference between Abstraction and Encapsulation


Abstraction is a process. It is the act of identifying the relevant qualities and behaviors an object should
possess. Encapsulation is the mechanism by which the abstraction is implemented.
Abstraction  Encapsulation
Abstraction solves the problem in the
Encapsulation solves the problem in the implementation level.
design level.
Abstraction is used for hiding the unwanted Encapsulation is hiding the code and data into a single unit to
data and giving only relevant data. protect the data from outer world.
Abstraction is set focus on the object Encapsulation means hiding the internal details or mechanics of
instead of how it does it. how an object does something.
Abstraction is outer layout in terms of
Encapsulation is inner layout in terms of implementation.
design. 
For Example: - Inner Implementation detail of a iPhone, how
For Example: - Outer Look of a iPhone, like
Display Screen are connect with each other using circuits
it has a display screen.

What is data hiding?


Here's an example:

public class Vehicle


{
private bool isEngineStarted;

private void StartEngine()


{
// Code here.
this.isEngineStarted = true;
}

public void GoToLocation(Location location)


{
if (!this.isEngineStarted)
{
this.StartEngine();
}

// Code here: move to a new location.


}
}
As you see, the isEngineStarted field is private, ie. accessible from the class itself. In fact,
when calling an object of type Vehicle, we do need to move the vehicle to a location, but
don't need to know how this will be done. For example, it doesn't matter, for the caller
object, if the engine is started or not: if it's not, it's to the Vehicle object to start it before
moving to a location.
Why do we need this?
Mostly to make the code easier to read and to use. Classes may have dozens or hundreds
of fields and properties that are used only by them. Exposing all those fields and properties
to the outside world will be confusing.

Another reason is that it is easier to control a state of a private field/property. For example,
in the sample code above, imagine StartEngine is performing some tasks, then
assigning true to this.isEngineStarted. If isEngineStarted is public, another class would be
able to set it to true, without performing tasks made by StartEngine. In this case, the value
of isEngineStarted will be unreliable.

SELECT * FROM STUDENT (NOLOCK)


---> Select Nth highest marks from table.
SELECT TOP 1 MARKS FROM (SELECT DISTINCT TOP 15 MARKS FROM STUDENT (NOLOCK)
ORDER BY MARKS DESC)A ORDER BY MARKS
---> Find the mahine name.
select serverproperty('MachineName')
SELECT SERVERPROPERTY(N'MachineName');
---> Find the sql server name
select @@servername
---> How to get Host name and SQL Instance Name by T-SQL
SELECT
SERVERPROPERTY('MachineName') AS [ServerName],
SERVERPROPERTY('ServerName') AS [ServerInstanceName],
SERVERPROPERTY('InstanceName') AS [Instance],
SERVERPROPERTY('Edition') AS [Edition],
SERVERPROPERTY('ProductVersion') AS [ProductVersion],
Left(@@Version, Charindex('-', @@version) - 2) As VersionName

---> Find Hostname and Current Logged In User Name


SELECT HOST_NAME() AS HostName, SUSER_NAME() LoggedInUser
---> How to: Create a Full Database Backup (Transact-SQL)
BACKUP DATABASE Temp TO DISK = 'E:\Personal\Temp.Bak'
--BACKUP DATABASE <myDataBaseName> TO DISK = 'C:\PathtoBackup\FileName.bak'
---> how to show only even or odd rows in sql server 2008
SELECT STU.FIRSTNAME, STU.LASTNAME, STU.CLASS, STU.MARKS
FROM (
SELECT *, Row_Number() OVER(ORDER BY MARKS) AS RowNumber
--Row_Number() starts with 1
FROM STUDENT
) STU
WHERE STU.RowNumber % 2 = 0 --Even
--WHERE STU.RowNumber % 2 = 1 –Odd

---> Query for Rank (RowNumber) and grouping


Select MARKS,
(Select 1 + Count(Distinct MARKS) -- "1 +" gives 1-based rank,
From STUDENT -- take it out to get 0-based rank
Where MARKS > A.MARKS) Rank
From STUDENT A
Order By MARKS

Select Id, Name


from Table1
where Name not in (select Name from Table2)
UNION
Select Id, Name
from Table2
where Name not in (select Name from Table1)
select a.Id, a.Name from Table1 a left outer join Table2 b on a.Name = b.Name where b.Id
is null
UNION ALL
select a.Id, a.Name from Table2 a left outer join Table1 b on a.Name = b.Name where b.Id
is null

---> Syntax to change the identity column value


set identity_insert [YourTable] ON
Then delete your row and reinsert it with different identity.

Once you have done the insert don't forget to turn identity_insert off

set identity_insert [YourTable] OFF

Query to identify database name using table name:


SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'tsConfigHistory'

Query to identify database name using stored procedure name:

EXEC sp_msforeachdb
'if exists(select 1 from [?].sys.objects where name=''loadDocuments'')
select ''?'' as FoundInDatabase from [?].sys.objects where name=''loadDocuments'''

Deleting all duplicate rows but keeping one [duplicate]


DELETE FROM Person WHERE ID NOT IN (SELECT MIN(ID) FROM Person
GROUP BY [LastName], [FirstName], [Discriminator])

Deleting all duplicate rows from table


DELETE FROM Person WHERE ID IN (SELECT ID FROM Person
GROUP BY ID, [LastName], [FirstName], [Discriminator])

Changing the Seed Value of Identity column


DBCC CHECKIDENT ('MVCTest.dbo.Person', RESEED, 10)

Basic Difference
1. Function must return a value but in Stored Procedure it is optional (Procedure can return zero or n values).
2. Functions can have only input parameters for it whereas Procedures can have input/output parameters.
3. Functions can be called from Procedure whereas Procedures cannot be called from Function.

Advance Difference
1. Procedure allows SELECT as well as DML (INSERT/UPDATE/DELETE) statement in it whereas Function allows
only SELECT statement in it.
2. Procedures cannot be utilized in a SELECT statement whereas Function can be embedded in a SELECT
statement.
3. Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section
whereas Function can be.
4. The most important feature of stored procedures over function is to retention and reuse the execution plan while
in case of function it will be compiled every time.
5. Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.
6. Inline Function can be though of as views that take parameters and can be used in JOINs and other Rowset
operations.
7. Exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a
Function.
8. We can go for Transaction Management in Procedure whereas we can't go in Function.

Differences between Stored Procedure and User Defined Function in SQL Server

Sr.No
User Defined Function Stored Procedure
.
1  Function must return a value. Stored Procedure may or not return values.

Will allow only Select statements, it will not Can have select statements as well as DML statements such
2
allow us to use DML statements. as insert, update, delete and so on
 It will allow only input parameters, doesn't
3 It can have both input and output parameters.
support output parameters.
4 It will not allow us to use try-catch blocks. For exception handling we can use try catch blocks.

5 Transactions are not allowed within functions. Can use transactions within Stored Procedures.

We can use only table variables, it will not


6 Can use both table variables as well as temporary table in it.
allow using temporary tables.
Stored Procedures can't be called from a
7 Stored Procedures can call functions.
function.
Procedures can't be called from Select/Where/Having and so
 Functions can be called from a select
8 on statements. Execute/Exec statement can be used to
statement.
call/execute Stored Procedure.
A UDF can be used in join clause as a result
9 Procedures can't be used in Join clause
set.

What is a Trigger
A trigger is a special kind of a store procedure that executes in response to certain action on the table
like insertion, deletion or updation of data. It is a database object which is bound to a table and is
executed automatically. You can’t explicitly invoke triggers. The only way to do this is by performing the
required action no the table that they are assigned to.

Types Of Triggers:
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So,
there are three types of triggers and hybrids that come from mixing and matching the events and
timings that fire them. Basically, triggers are classified into two main types:

i. After Triggers (For Triggers)


ii. Instead Of Triggers
(i) After Triggers
These triggers run after an insert, update or delete on a table. They are not supported for views. 
AFTER TRIGGERS can be classified further into three types as:
a. AFTER INSERT Trigger.
b. AFTER UPDATE Trigger.
c. AFTER DELETE Trigger.

Let’s create After triggers. First of all, let’s create a table and insert some sample data. Then, on this
table, I will be attaching several triggers.

CREATE TABLE Employee_Test


(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee_Test VALUES ('Anees',1000);


INSERT INTO Employee_Test VALUES ('Rick',1200);
INSERT INTO Employee_Test VALUES ('John',1100);
INSERT INTO Employee_Test VALUES ('Stephen',1300);
INSERT INTO Employee_Test VALUES ('Maria',1400);

I will be creating an AFTER INSERT TRIGGER which will insert the rows inserted into the table into
another audit table. The main purpose of this audit table is to record the changes in the main table.
This can be thought of as a generic audit trigger.

Now, create the audit table as:-

Hide   Copy Code
CREATE TABLE Employee_Test_Audit
(
Emp_ID int,
Emp_name varchar(100),
Emp_Sal decimal (10,2),
Audit_Action varchar(100),
Audit_Timestamp datetime
)

(a) After Insert Trigger


This trigger is fired after an INSERT on the table. Let’s create the trigger as:

Hide   Copy Code
CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test]
FOR INSERT
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);

select @empid=i.Emp_ID from inserted i;


select @empname=i.Emp_Name from inserted i;
select @empsal=i.Emp_Sal from inserted i;
set @audit_action='Inserted Record -- After Insert Trigger.';

insert into Employee_Test_Audit


(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate());

PRINT 'AFTER INSERT trigger fired.'


GO

The CREATE TRIGGER statement is used to create the trigger. THE ON clause specifies the table
name on which the trigger is to be attached. The FOR INSERT specifies that this is an AFTER
INSERT trigger. In place of FOR INSERT, AFTER INSERT can be used. Both of them mean the same.

In the trigger body, table named inserted has been used. This table is a logical table and contains
the row that has been inserted. I have selected the fields from the logical inserted table from the row
that has been inserted into different variables, and finally inserted those values into the Audit table.

To see the newly created trigger in action, lets insert a row into the main table as:

Hide   Copy Code
insert into Employee_Test values('Chris',1500);

Now, a record has been inserted into the Employee_Test table. The AFTER INSERT trigger attached
to this table has inserted the record into the Employee_Test_Audit as:

Hide   Copy Code
6 Chris 1500.00 Inserted Record -- After Insert Trigger. 2008-04-26 12:00:55.700

(b) AFTER UPDATE Trigger


This trigger is fired after an update on the table. Let’s create the trigger as:

Hide   Copy Code
CREATE TRIGGER trgAfterUpdate ON [dbo].[Employee_Test]
FOR UPDATE
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);
select @empid=i.Emp_ID from inserted i;
select @empname=i.Emp_Name from inserted i;
select @empsal=i.Emp_Sal from inserted i;

if update(Emp_Name)
set @audit_action='Updated Record -- After Update Trigger.';
if update(Emp_Sal)
set @audit_action='Updated Record -- After Update Trigger.';

insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)


values(@empid,@empname,@empsal,@audit_action,getdate());

PRINT 'AFTER UPDATE Trigger fired.'


GO

The AFTER UPDATE Trigger is created in which the updated record is inserted into the audit table.
There is no logical table updated like the logical table inserted. We can obtain the updated value
of a field from theupdate(column_name) function. In our trigger, we have used, if
update(Emp_Name) to check if the column Emp_Name has been updated. We have similarly
checked the column Emp_Sal for an update.

Let’s update a record column and see what happens.

update Employee_Test set Emp_Sal=1550 where Emp_ID=6

This inserts the row into the audit table as:

6 Chris 1550.00 Updated Record -- After Update Trigger. 2008-04-26 12:38:11.843

(c) AFTER DELETE Trigger


This trigger is fired after a delete on the table. Let’s create the trigger as:

Hide   Copy Code
CREATE TRIGGER trgAfterDelete ON [dbo].[Employee_Test]
AFTER DELETE
AS
declare @empid int;
declare @empname varchar(100);
declare @empsal decimal(10,2);
declare @audit_action varchar(100);

select @empid=d.Emp_ID from deleted d;


select @empname=d.Emp_Name from deleted d;
select @empsal=d.Emp_Sal from deleted d;
set @audit_action='Deleted -- After Delete Trigger.';

insert into Employee_Test_Audit


(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate());

PRINT 'AFTER DELETE TRIGGER fired.'


GO

In this trigger, the deleted record’s data is picked from the logical deleted table and inserted into
the audit table. Let’s fire a delete on the main table. A record has been inserted into the audit table
as:

6 Chris 1550.00 Deleted -- After Delete Trigger. 2008-04-26 12:52:13.867

All the triggers can be enabled/disabled on the table using the statement

ALTER TABLE Employee_Test {ENABLE|DISBALE} TRIGGER ALL

Specific Triggers can be enabled or disabled as:

ALTER TABLE Employee_Test DISABLE TRIGGER trgAfterDelete

This disables the After Delete Trigger named trgAfterDelete on the specified table.

(ii) Instead Of Triggers


These can be used as an interceptor for anything that anyone tried to do on our table or view. If you
define anInstead Of trigger on a table for the Delete operation, they try to delete rows, and they will
not actually get deleted (unless you issue another delete instruction from within the trigger)

INSTEAD OF TRIGGERS can be classified further into three types as:

a. INSTEAD OF INSERT Trigger.


b. INSTEAD OF UPDATE Trigger.
c. INSTEAD OF DELETE Trigger.

Let’s create an Instead Of Delete Trigger as:

CREATE TRIGGER trgInsteadOfDelete ON [dbo].[Employee_Test]


INSTEAD OF DELETE
AS
declare @emp_id int;
declare @emp_name varchar(100);
declare @emp_sal int;

select @emp_id=d.Emp_ID from deleted d;


select @emp_name=d.Emp_Name from deleted d;
select @emp_sal=d.Emp_Sal from deleted d;

BEGIN
if(@emp_sal>1200)
begin
RAISERROR('Cannot delete where salary > 1200',16,1);
ROLLBACK;
end
else
begin
delete from Employee_Test where Emp_ID=@emp_id;
COMMIT;
insert into
Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@emp_id,@emp_name,@emp_sal,'Deleted -- Instead Of Delete
Trigger.',getdate());
PRINT 'Record Deleted -- Instead Of Delete Trigger.'
end
END
GO

This trigger will prevent the deletion of records from the table where Emp_Sal > 1200. If such a
record is deleted, the Instead Of Trigger will rollback the transaction, otherwise the transaction will
be committed. Now, let’s try to delete a record with the Emp_Sal >1200 as:

delete from Employee_Test where Emp_ID=4

This will print an error message as defined in the RAISE ERROR statement as:

Server: Msg 50000, Level 16, State 1, Procedure trgInsteadOfDelete, Line 15


Cannot delete where salary > 1200

And this record will not be deleted.

Question: What are magic tables in sql server?

Answer: Magic tables are nothing but inserted and deleted which are temporary object created by
server internally to hold the recently inserted values in the case of insert and to hold recently deleted
values in the case of delete, to hold before updating values or after updating values in the case of
update.

Question: How many types of Magic tables available in sql server?

Answer: 1) Inserted 2) Deleted

SELECT 15 AS RESULT UNION ALL


SELECT $ AS RESULT UNION ALL
SELECT COUNT(*) AS RESULT UNION ALL
SELECT COUNT('7') AS RESULT UNION ALL
--SELECT 'JAI BAJRANG' + 1 AS RESULT UNION ALL -- THROW AN ERROR
SELECT 'JAI BAJRANG' + '1' AS RESULT UNION ALL
SELECT (SELECT 'JAI BAJRANG') AS RESULT UNION ALL
--SELECT SELECT 'JIA BAJRANG' AS RESULT UNION ALL -- THROW AN ERROR
--SELECT * FROM 'COUNTRY' AS RESULT UNION ALL -- THROW AN ERROR
--SELECT * FROM TABLE1, TABLE2 AS RESULT UNION ALL
SELECT COUNT(*) + COUNT(*) AS RESULT UNION ALL -- 2
SELECT 'JAI BAJRANG' FROM Table1 AS RESULT UNION ALL
SELECT SUM(1+2*3) AS RESULT UNION ALL
SELECT MAX(1+2*3) AS RESULT UNION ALL
--SELECT MAX(1, 3, 4) AS RESULT UNION ALL -- THROW AN ERROR
SELECT MAX('JAI BAJRANG') AS RESULT UNION ALL
--SELECT COUNT(SELECT ID FROM Table1) AS RESULT UNION ALL -- THROW AN ERROR
SELECT 1 + '1' AS RESULT UNION ALL -- RESULT IS 2
SELECT '1' + 1 AS RESULT UNION ALL -- RESULT IS 2
SELECT NULL + 5 AS RESULT UNION ALL -- RESULT IS NULL
SELECT NULL + '1' AS RESULT UNION ALL -- RESULT IS NULL
SELECT 1 AS RESULT WHERE NULL = NULL UNION ALL --NOTHING WILL RETURN BY THIS QUERY.
SELECT SUM(1) AS RESULT UNION ALL -- RESULT IS 1
--SELECT SUM('1') AS RESULT UNION ALL -- THROW ERROR
--SELECT SUM(NULL) AS RESULT UNION ALL -- THROW ERROR
SELECT 6/0 AS RESULT UNION ALL -- THROW ERROR "Divide by zero error encountered."
SELECT 0/0 AS RESULT UNION ALL -- THROW ERROR "Divide by zero error encountered."
SELECT 0/9 AS RESULT -- RESULT IS 0

CREATE TABLE ExampleTable2 (PriKey INT PRIMARY KEY, VerCol ROWVERSION)


CREATE TABLE ExampleTable1 (PriKey INT PRIMARY KEY, VerCol TIMESTAMP)

SELECT 1 + GETDATE() + 1
SELECT TOP 1 * FROM Table1 ORDER BY NEWID()

Interfaces are contracts that implementers must follow. Abstract classes allow contracts


plus shared implementations - something that Interfaces cannot have. Classes can
implement and inherit multiple interfaces. Classes can only extend a single abstract class.
Why Interface

 You don't have default or shared code implementation


 You want to share data contracts (web services, SOA)
 You have different implementations for each interface implementer
(IDbCommand has SqlCommandand OracleCommand which implement the interface in specific
ways)
 You want to support multiple inheritance.
Why Abstract

 You have default or shared code implementation


 You want to minimize code duplication
 You want to easily support versioning

What is the difference between IQueryable<T> and IEnumerable<T>?


The primary difference is that the LINQ operators for IQueryable<T> take Expression objects
instead of delegates, meaning the custom query logic it receives, e.g., a predicate or value
selector, is in the form of an expression tree instead of a delegate to a method.
 IEnumerable<T> is great for working with sequences that are iterated in-memory, but
 IQueryable<T> allows for out-of memory things like a remote data source, such as a
database or web service.
Query execution:

 Where the execution of a query is going to be performed "in process", typically all


that's required is the code (as code) to execute each part of the query.
 Where the execution will be performed out-of-process, the logic of the query has to be
represented in data such that the LINQ provider can convert it into the appropriate form
for the out-of-memory execution - whether that's an LDAP query, SQL or whatever.

37) What are HttpHandlers?

ASP.NET programming supports the creation of custom HttpHandler components, which


provide an efficient way to process requests that don't return standard HTML-based pages.

E.g. : HttpHandler components are good for situations in which you want to return XML,
simple text or binary data to the user.

The easiest way to create a custom HttpHandler component is to create a source file with


an .ashx extension. You must then add a @WebHandler directive to the top of the .ashx file
with a class definition that implements the IHttpHandler interface.

74) What is cross-page posting?

Server.Transfer() method is used for posting the data from one page to another.

In cross page posting, data collected from different pages and will be displayed in single
page. So, for doing this we need to set “PostBackUrl” property of the control, in which
target page is specified and in target page we can use “PreviousPage” property. For
doing this we need to set the directive - @PreviousPageType. Previous page control can
be accessed from the method – “FindControl()”.

89) What exactly happens when ASPX page is requested from Browser?

Following are the steps which will occur when we request an ASPX page from web
server –

 The browser sends the request to the webserver. Let’s assume the webserver at the
other end is IIS.
 Once IIS receives the request, it looks for engine where it can serve this request.
When engine means it’s the DLL which can parse this page or compile and send a
response back to browser. The request which is to be mapped is decided by file
extension of the page requested.
Some File extension mapping given below -

 .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

92) What is the use of "HttpHandlers" in web.config ?


HttpHandler will be called once the request comes from client machine. Example:
one .aspx or .asmx file is requested.

<configuration>
<system.web>
<compilation debug="true"/>
<httpHandlers>
<add verb="*" path="*.xsl" type="Handler" />
</httpHandlers>
</system.web>
</configuration>

Mapping will be done for the requests to appropriate handlers based on URL and HTTP
request verb.

53) How can we determine action invoked from HTTP GET or HTTP POST?

This can be done in following way –

 Use class – “HttpRequestBase” and use the method – “HttpMethod” to determine the
action request type.

if (Request.HttpMethod == "POST")
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Operation()
{
// insert here the GET logic
return SomeView(...)
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Operation(SomeModel model)
{
// insert here the POST logic
return SomeView(...);
}

You might also like