locked
How to Create REST service in .NET RRS feed

  • Question

  • User602630008 posted

    hi..

    i want to know how to create rest service using .net?

    how to create rest service.


    give some examples for REST service in .net.

    regards
    gopal.s

    Friday, May 24, 2013 5:26 AM

Answers

  • User1462500545 posted

    IService Interface Code

    [ServiceContract]
        public interface IService
        {
            [OperationContract]
            [WebInvoke(UriTemplate = "/Category/{categoryID}", Method = "GET")]
            Category GetCategory(string CategoryID);
            [OperationContract]
            [WebInvoke(UriTemplate = "/Category", Method = "GET")]
            List
    <category /> GetCategories();
            [OperationContract]
            [WebInvoke(UriTemplate = "/Category/Add", Method = "POST")]
            bool AddCategory(Category category);
        }

    Service Code


    public class Service : IService
        {
            public Category GetCategory(string categoryID)
            {
                NorthwindDataContext context = new NorthwindDataContext();
                var category = context.Categories.SingleOrDefault(e => e.CategoryID == Convert.ToInt32(categoryID));
                context.Dispose();
                return category;
            }
            public List
    <category /> GetCategories()
            {
                NorthwindDataContext context = new NorthwindDataContext();
                var categories = context.Categories.ToList();
                context.Dispose();
                return categories;
            }
            public bool AddCategory(Category category)
            {
                NorthwindDataContext context = new NorthwindDataContext();
                context.Categories.InsertOnSubmit(category);
                context.SubmitChanges();
                context.Dispose();
                return true;
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 24, 2013 8:47 AM

All replies

  • User1462500545 posted

    IService Interface Code

    [ServiceContract]
        public interface IService
        {
            [OperationContract]
            [WebInvoke(UriTemplate = "/Category/{categoryID}", Method = "GET")]
            Category GetCategory(string CategoryID);
            [OperationContract]
            [WebInvoke(UriTemplate = "/Category", Method = "GET")]
            List
    <category /> GetCategories();
            [OperationContract]
            [WebInvoke(UriTemplate = "/Category/Add", Method = "POST")]
            bool AddCategory(Category category);
        }

    Service Code


    public class Service : IService
        {
            public Category GetCategory(string categoryID)
            {
                NorthwindDataContext context = new NorthwindDataContext();
                var category = context.Categories.SingleOrDefault(e => e.CategoryID == Convert.ToInt32(categoryID));
                context.Dispose();
                return category;
            }
            public List
    <category /> GetCategories()
            {
                NorthwindDataContext context = new NorthwindDataContext();
                var categories = context.Categories.ToList();
                context.Dispose();
                return categories;
            }
            public bool AddCategory(Category category)
            {
                NorthwindDataContext context = new NorthwindDataContext();
                context.Categories.InsertOnSubmit(category);
                context.SubmitChanges();
                context.Dispose();
                return true;
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 24, 2013 8:47 AM