- Select File -> New Project in Visual Studio 2013. I am using Visual Studio Express 2013 for Web
- Select Visual C# under templates and ASP.NET Web Application under projects
- Give a name to project and select location where you want to create this project
- Click on Ok to get to next screen
- Select Web API as the template. This automatically selects MVC and Web API checkboxes at the bottom of this window
- Click on Change Authentication button and select No Authentication in Change Authentication window
- Your window should look like below. Click on Ok to create project
- Select App_Data folder in Solution Explorer. Select Project -> New Items -> Sql Server Database. Change database
name to Contacts.Mdf
- Double click on Contacts.Mdf to open in Server Explorer. Select Database and choose New Query option from popup menu
- Create CONTACTS table and insert some sample data by giving the following commands in Query Window and run them
create table Contacts
( Name varchar(50) primary key,
Email varchar(50),
Mobile varchar(30)
);
insert into Contacts values ('Srikanth Pragada','srikanthpragada@yahoo.com','9059057000');
insert into Contacts values ('Praneeth Pragada','praneethpragada@yahoo.com','8020081000');
- Go to Models folder and add the following classes related LINQ To SQL
Contact.cs
using System.Data.Linq.Mapping;
using System.Linq;
namespace contacts_webapi.Models
{
[Table (Name="Contacts")]
public class Contact
{
[Column (IsPrimaryKey = true)]
public string Name { get; set; }
[Column]
public string Email { get; set; }
[Column]
public string Mobile { get; set; }
}
}
ContactsDataContext.cs
using System.Data.Linq;
using System.Linq;
namespace contacts_webapi.Models
{
public class ContactsDataContext : DataContext
{
public ContactsDataContext()
: base(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|datadirectory|\Contacts.mdf;Integrated Security=True")
{
}
public Table<Contact> Contacts {
get {
return GetTable<Contact>();
}
}
}
}
- Go to Controllers folder and create a new Web API using Project -> Add New Item -> Web API Controller Class (v2). Write the following code
in that.
using contacts_webapi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace contacts_webapi.Controllers
{
public class ContactsController : ApiController
{
// GET all contacts
public IEnumerable<Contact> Get()
{
ContactsDataContext ctx = new ContactsDataContext();
return ctx.Contacts;
}
// GET contact details for a single contact by name (id is name)
public Contact Get(String id)
{
ContactsDataContext ctx = new ContactsDataContext();
var contact = (from c in ctx.Contacts
where c.Name.ToUpper() == id.ToUpper()
select c).SingleOrDefault();
if ( contact == null)
{
// send 404 error
HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(msg);
}
return contact;
}
// Insert new contact
public void Post(Contact contact)
{
try
{
ContactsDataContext ctx = new ContactsDataContext();
ctx.Contacts.InsertOnSubmit(contact);
ctx.SubmitChanges();
}
catch (Exception ex)
{
// send 500 error
HttpResponseMessage msg = new HttpResponseMessage();
msg.StatusCode = HttpStatusCode.InternalServerError;
msg.ReasonPhrase = "Error while adding contact -> " + ex.Message;
throw new HttpResponseException(msg);
}
}
// Update existing contact - id is name and contact contains new data
public void Put(string id, Contact contact)
{
ContactsDataContext ctx = new ContactsDataContext();
var dbcontact = (from c in ctx.Contacts
where c.Name.ToUpper() == id.ToUpper()
select c).SingleOrDefault();
if (dbcontact != null)
{
dbcontact.Email = contact.Email;
dbcontact.Mobile = contact.Mobile;
ctx.SubmitChanges();
}
else
{
// send 404 error
HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(msg);
}
}
// delete an existing contact by name - id is name here
public void Delete(string id)
{
ContactsDataContext ctx = new ContactsDataContext();
var contact = (from c in ctx.Contacts
where c.Name.ToUpper() == id.ToUpper()
select c).SingleOrDefault();
if (contact != null)
{
ctx.Contacts.DeleteOnSubmit(contact);
ctx.SubmitChanges();
}
else
{
// send 404 error
HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.NotFound);
throw new HttpResponseException(msg);
}
}
}
}
As we have created required classes for LINQ to SQL and Web API, lets now move on to a simple HTML page that uses AngularJS to
consume Web API.
Comments
|
Posted By Chander On 19-May-15 12:19:38 AM
Nice. Kindly provide ASP.NET MVC materials(articles,faqs,online exam)
|
|
Posted By amberwoods24.freeblog.biz On 08-May-20 08:22:22 PM
Really appreciate you sharing this blog. Keep writing.
|