Question

I want use WCF WebApi in a regular ASP.NET C# project. Already I have created WCF WebApi in MVC application but I want to create in normal ASP.NET. Are there any sample links to show this?

Was it helpful?

Solution

File / New Project / ASP.NET Application

NuGet: Install-Package WebApi.All

Add a new ContactsResource

[ServiceContract]
public class ContactsResource {
    [WebGet(UriTemplate = "")] 
    public List<Contact> Get() {
        return new List<Contact>()
                {
                    new Contact()
                        {
                            Name = "Alex"
                        }
                };
     }
}

Add a Contact class

public class Contact {
    public string Name { get; set; }
}

Edit the Global.asax.cs

Modify Application_Start:

void Application_Start(object sender, EventArgs e) {
    // Code that runs on application startup
    RouteTable.Routes.MapServiceRoute<ContactsResource>("contacts");
}

Hit F5 and navigate to http://mywebsite/contacts

Done.

<ArrayOfContact>
    <Contact>
        <Name>Alex</Name>
    </Contact>
 </ArrayOfContact>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top