1

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?

ckittel
  • 6,478
  • 3
  • 41
  • 71
Victor
  • 555
  • 6
  • 15
  • 39
  • There are several flavors of WCF - which one are you talking about? Are you looking for one that uses SOAP, or REST? Please clarify your question as to what you're looking for. – Tim Sep 07 '11 at 05:40
  • Hi thank you for giving response.http://wcf.codeplex.com/wikipage?title=WCF%20HTTP i have fallow above link for webapi please check it once – Victor Sep 07 '11 at 05:52
  • So you want to do essentially the same thing in the tutorial, only using Web Forms instead of MVC? – Tim Sep 07 '11 at 06:01
  • yes Tim please give any link if you know ... – Victor Sep 07 '11 at 06:03
  • I couldn't find any with a quick Google search. You could try it yourself (consider it a learning opportunity) :). You also might want to take a look at [Introducing WCF WebHttp Services in .NET 4](http://blogs.msdn.com/b/endpoint/archive/2010/01/06/introducing-wcf-webhttp-services-in-net-4.aspx) - it *might* be of value, though it's not the Web API. – Tim Sep 07 '11 at 06:08
  • If you run into specific issues, feel free to post questions - just show the code you have that isn't working and someone here should be able to give you an answer. Good luck :) – Tim Sep 07 '11 at 07:29

1 Answers1

5

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>
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124