How to Create OData Service in ASP.NET

In this article you will learn how to create an OData Service in ASP.Net.

First of all start Visual Studio and create a new website and provide the name and location and click "Next" then click on "Add new item" and select "ADO.NET Entity Data Model" from installed templates and click "Add".

img1.jpg

Image 1.

And the next step is to choose "Generate from database" and click the "Next" button and after that choose your data connection.

img2.jpg

Image 2.

Now select table names and objects and click "Finish".

img3.jpg

Image 3.

Now right-click on the solution and click on "Add new item" and select "WCF Data Service".

img4.jpg

Image 4.

This is the default class code.

public class CustomersService : DataService< /* TODO: put your data source class name here */ >

{

    // This method is called only once to initialize service-wide policies.

    public static void InitializeService(DataServiceConfiguration config)

    {

        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

        // Examples:

        // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;

    }

}

You need to make a few changes like the following:

public class CustomersService : DataService<NORTHWNDEntities1>

{

    // This method is called only once to initialize service-wide policies.

    public static void InitializeService(DataServiceConfiguration config)

    {

        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

        // Examples:

        config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);

        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;

    }

}

Now right-click on the service and click on "View in browser".

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<service xml:base="http://localhost:5376/CustomersService.svc/">

  <workspace>

    <atom:title>Default</atom:title>

    <collection href="Customers">

      <atom:title>Customers</atom:title>

    </collection>

  </workspace>

</service>


Up Next
    Ebook Download
    View all
    Learn
    View all