ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available. SignalR supports Web Sockets, and falls back to other compatible techniques for older browsers. SignalR includes APIs for connection management (for instance, connect and disconnect events), grouping connections, and authorization.
Click here to Start learning SignalR application
The .NET Framework and ASP.NET help you create web applications and services for Windows.
Friday, June 13, 2014
Thursday, June 12, 2014
Simple Example of Web API in Asp.net
I am creating a very simple example which displays list of customer.
Steps:
1. Create Asp.net Empty Web Application.
2. Create Customer class like this
In Web API, a controller is an object that handles HTTP requests.
Create CustomersController class (Add "Web API Controller Class" and remvoe extra stuff) like this.
In this section, we'll add an HTML page that uses AJAX to call the web API. We'll use jQuery to make the AJAX calls and also to update the page with the results.
Create HTML page (index.html) like this
Steps:
1. Create Asp.net Empty Web Application.
2. Create Customer class like this
Public Class Customer
Public Property Name As String
Public Property City As String
End Class
3. Adding a Controller.In Web API, a controller is an object that handles HTTP requests.
Create CustomersController class (Add "Web API Controller Class" and remvoe extra stuff) like this.
Imports System.Net
Imports System.Web.Http
Public Class CustomersController
Inherits ApiController
' GET api/<controller> '
Public Function GetAllCustomers() As IEnumerable(Of Customer)
'Creating test data '
Dim customers As New List(Of Customer)
Dim objCustomer As New Customer
objCustomer.Name = "Miller"
objCustomer.City = "Camp Hill"
customers.Add(objCustomer)
objCustomer = New Customer
objCustomer.Name = "John"
objCustomer.City = "Harrisburg"
customers.Add(objCustomer)
objCustomer = New Customer
objCustomer.Name = "Wayne"
objCustomer.City = "Enola"
customers.Add(objCustomer)
objCustomer = New Customer
objCustomer.Name = "Julie"
objCustomer.City = "Enola"
customers.Add(objCustomer)
Return customers
End Function
End Class
4. Calling the Web API with Javascript and jQuery In this section, we'll add an HTML page that uses AJAX to call the web API. We'll use jQuery to make the AJAX calls and also to update the page with the results.
Create HTML page (index.html) like this
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div>
<h2>All Customers</h2>
<ul id="customers" ></ul>
</div>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var uri = 'api/customers';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#customers'));
});
});
});
function formatItem(item) {
return item.Name + ': ' + item.City;
}
</script>
</body>
</html>
5. Create Global.asax page like thisImports System.Web.SessionState
Imports System.Web.Http
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim route As New Routing.HttpRoute("api/{controller}")
GlobalConfiguration.Configuration.Routes.Add("DefaultApi", route)
End Sub
End Class
That's it! When you run your web application. Result will be: All Customers
- Miller: Camp Hill
- John: Harrisburg
- Wayne: Enola
- Julie: Enola
Project Structure:
Wednesday, June 11, 2014
What is Web API in Asp.net?
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
Click here to see simple example of Web API in Asp.net
Click here to see simple example of Web API in Asp.net
What is Windows Communication Foundation (WCF)?
Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments.
What is LINQ (Language-Integrated Query)?
Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.
Simple example on LINQ:
All LINQ query operations consist of three distinct actions:
1.Obtain the data source.
2.Create the query.
3.Execute the query.
Simple example on LINQ:
All LINQ query operations consist of three distinct actions:
1.Obtain the data source.
2.Create the query.
3.Execute the query.
Public Class Customer
Public Property Name As String
Public Property City As String
End Class
'The Three Parts of a LINQ Query '
'1. Data source '
Dim customers As New List(Of Customer)
Dim objCustomer As New Customer
objCustomer.Name = "Miller"
objCustomer.City = "Camp Hill"
customers.Add(objCustomer)
objCustomer = New Customer
objCustomer.Name = "John"
objCustomer.City = "Harrisburg"
customers.Add(objCustomer)
objCustomer = New Customer
objCustomer.Name = "Wayne"
objCustomer.City = "Enola"
customers.Add(objCustomer)
objCustomer = New Customer
objCustomer.Name = "Julie"
objCustomer.City = "Enola"
customers.Add(objCustomer)
'2. Query creation '
Dim enolaCustomers = From oCustomer As Customer In customers _
Where oCustomer.City = "Enola" _
Select oCustomer
'3. Query Execution '
For Each oCustomer As Customer In enolaCustomers
Response.Write(oCustomer.Name + "<br/>")
Next
'Result will be :'
Wayne
Julie
What is Asp.net MVC?
MVC stands for model-view-controller. MVC is a pattern for developing applications that are well architected, testable and easy to maintain. MVC-based applications contain:
- Models: Classes that represent the data of the application and that use validation logic to enforce business rules for that data.
- Views: Template files that your application uses to dynamically generate HTML responses.
- Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.
Entity Framework Development Workflows
Entity Framework supports four basic development workflows. You can choose which one is appropriate for you based on following questions.
1. Do you have Existing Database? or
2. Are you going to create New Database?
New Database:
There are two approaches for New Database:
1. Model First
Existing Database:
There are two approaches for Existing Database:
1. Database First
1. Do you have Existing Database? or
2. Are you going to create New Database?
New Database:
There are two approaches for New Database:
1. Model First
- Create model in designer
- Database created from model
- Classes auto-generated from model
- Define classes & mapping in code
- Database created from defined classes
- Use Migrations to evolve database
Existing Database:
There are two approaches for Existing Database:
1. Database First
- Reverse engineer model in designer
- Classes auto-generated from model
- Define classes & mapping in code
- Reverse engineer tools available
What is Entity Framework?
Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.
Entity Framework allows you to create a model by writing code or using boxes and lines in the EF Designer. Both of these approaches can be used to target an existing database or create a new database.
Entity Framework: Four Development Workflows
Entity Framework allows you to create a model by writing code or using boxes and lines in the EF Designer. Both of these approaches can be used to target an existing database or create a new database.
Entity Framework: Four Development Workflows
Tuesday, June 10, 2014
How to keep jQuery UI Accordion collapsed by default?
You can do that by setting attribute -> "active: false".
<script>
$(function () {
$("#accordion").accordion({
collapsible: true,
active: false
});
});
</script>
How to increase Height of accordion 100% in JQuery?
100% means height should be expanded based on height of the content. You can do that by following way.
<script>
$(function () {
$("#accordion").accordion({
collapsible: true,
heightStyle: "content"
});
});
</script>
Monday, June 9, 2014
How to create ErrorHandling Module Using IHttpModule?
I am going to explain IHttpModule using an example here. Imagine that you don’t want to write error handling code in each and every page/class instead you want to create separate “Error Handling” module in your project without touching “Global.asax” file. You can do so by implementing “IHttpModule” interface. Following example shows you very simple code about handling error globally. I am attaching some html in beginning and End of the request. And also in between I am showing the error message occurred anywhere in the project. So here is the sample code.
Steps:
1. Create a class “ErrorHandlingModule” in “App_Code” folder of your project
2. Implement “IHttpModule” in this class. When you press enter button at the end of “IHttpModule”, It will implement two methods “Dispose() and Init()”.
3. In Init(), I have added three handlers “Error, BeginRequest, EndRequest”.
4. To catch all requests and error, you have to subscribe those in “Init” and create methods in this class.
5. I have created three methods “Application_BeginRequest”, ”Application_Error”, ”Application_EndRequest”.
6. In “Application_Error” Method, just for example, I have assigned error message to “ErrorMessage” property. I have created this property just for that.
7. Now, just to see how it works, I have created one .aspx page and its Page_Load event, I have thrown exception as follows:
When you run this page, error will be caught by our ErrorHandlingModule and you can see the result as below. You can log any error in the database as well as you can send an email to admin.
Public Class ErrorHandlingModule
Implements IHttpModule
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
Private _ErrorMessage As String
Public Property ErrorMessage() As String
Get
Return _ErrorMessage
End Get
Set(ByVal value As String)
_ErrorMessage = value
End Set
End Property
Public Sub Init(context As HttpApplication) Implements IHttpModule.Init
'Adding handler for Error '
AddHandler context.Error, _
AddressOf Me.Application_Error
'Adding Begin Request Handler '
AddHandler context.BeginRequest, _
AddressOf Me.Application_BeginRequest
'Adding End Request Handler '
AddHandler context.EndRequest, _
AddressOf Me.Application_EndRequest
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim application As HttpApplication = DirectCast(sender, _
HttpApplication)
Dim context As HttpContext = application.Context
context.Response.Write("<h3><font color=red>" & _
"ErrorHandlingModule: Beginning of Request" & _
"</font></h3><hr/><br/><br/>")
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
'Handling Error: This is just an example '
'In real time, you have to log your error in database '
'as well as send email to admin '
Dim LastException As Exception = HttpContext.Current.Server.GetLastError
If LastException IsNot Nothing Then
If LastException.InnerException IsNot Nothing Then
ErrorMessage = LastException.InnerException.Message
End If
End If
'Clear Error after handling it '
HttpContext.Current.Server.ClearError()
End Sub
Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim application As HttpApplication = DirectCast(sender, _
HttpApplication)
Dim context As HttpContext = application.Context
context.Response.Write(ErrorMessage + "<br/><br/><hr/><h3/><font color=red>" & _
"ErrorHandlingModule: End of Request</font></h3>")
End Sub
End Class
Steps:
1. Create a class “ErrorHandlingModule” in “App_Code” folder of your project
2. Implement “IHttpModule” in this class. When you press enter button at the end of “IHttpModule”, It will implement two methods “Dispose() and Init()”.
3. In Init(), I have added three handlers “Error, BeginRequest, EndRequest”.
4. To catch all requests and error, you have to subscribe those in “Init” and create methods in this class.
5. I have created three methods “Application_BeginRequest”, ”Application_Error”, ”Application_EndRequest”.
6. In “Application_Error” Method, just for example, I have assigned error message to “ErrorMessage” property. I have created this property just for that.
7. Now, just to see how it works, I have created one .aspx page and its Page_Load event, I have thrown exception as follows:
Public Class Home
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Throw New Exception("This is test ERROR")
End Sub
End Class
When you run this page, error will be caught by our ErrorHandlingModule and you can see the result as below. You can log any error in the database as well as you can send an email to admin.
Subscribe to:
Posts (Atom)
React-select is very slow on larger list - Found solution - using react-window
I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...
-
Today I will show you how to use new salesforce toolkit for .NET Application. Follows the steps: Step 1. Create a project with C# ASP.NET ...
-
Classic mode is a mode where IIS only works with ISAPI extensions and ISAPI filters directly. In fact, in this mode, ASP.NET is just an ISA...


