18

I am currently following this guide -> Link to asp.net website

As the guide says I added all the necessary packages via the nuget console and added the necessary usings to the WebApIConfig file. . But when I added the endpoint register method VS gave me an error.

The method I added:

public static void Register(HttpConfiguration config)
    {
        // New code:
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());
    }

The Error VS gave me:

Error   1   'System.Web.Http.HttpConfiguration' does not contain a definition for 'MapODataServiceRoute' and no extension method 'MapODataServiceRoute' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?) C:\Users\rvcamp\Desktop\odataTest\odataTest\App_Start\WebApiConfig.cs   29  20  odataTest

I checked the comments of the guide but this error is not mentioned, also I can not resolve the error either. What am I doing wrong?

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
Robin
  • 2,704
  • 7
  • 30
  • 47
  • Nevermind it is solved now. I reinstalled the odata nugget and rebuild the project. – Robin Dec 01 '14 at 09:00
  • I will never understand why microsoft omits using namespaces in their sample code. It slows down every guide by 2x. – Gojira Aug 25 '21 at 22:45

6 Answers6

22

I just had this problem. Very frustrating.

I resolved it by adding this in the references at the top of the code page

using System.Web.OData.Extensions;

Right clicking the method did not bring up the resolve menu item either.

Reinstalling everything did no resolve anything for me.

Watson
  • 1,385
  • 1
  • 15
  • 36
  • Yes I did this as well, still find it very odd. – Robin Dec 18 '14 at 13:00
  • Oh definitely, one would think if you right-click on 'MapODataServiceRoute' method in code it would automatically resolve the issue by importing the namespace. But no :( – Watson Dec 19 '14 at 00:51
  • 2
    Correct namespace for OData v4 is `using System.Web.Http.OData.Extensions;` – Jan Zahradník Sep 01 '16 at 09:40
  • 2
    For `Microsoft.AspNet.OData 7.0.0` see @Amir answer below. Very helpful for the present time. – Jim Aug 22 '18 at 19:43
14

MapODataServiceRoute is available in Routes Collection, hence below code will do

config.Routes.MapODataServiceRoute(
"odata",
 null, 
GetEdmModel(), 
new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
BashaG
  • 149
  • 1
  • 3
  • 6
    This depends: For OData3 you call `config.Routes.MapODataServiceRoute` and for OData4 you use `config.MapODataServiceRoute` (and for OData1-2 you use `config.Routes.MapODataRoute`). – springy76 Jun 02 '16 at 13:53
12

FOR OData V3

  1. Install Microsoft.AspNet.WebApi.OData
  2. Add using System.Web.Http.OData.Builder; and using System.Web.Http.OData.Extensions;
  3. use like config.Routes.MapODataServiceRoute(...)

FOR OData V4

  1. Install Microsoft.AspNet.OData
  2. Add using System.Web.OData.Builder; and using System.Web.OData.Extensions;
  3. use like config.MapODataServiceRoute(...)

Dont get stuck on WebApi word, they are both for web api.

stratovarius
  • 3,720
  • 1
  • 30
  • 26
6

If you have upgraded to Microsoft.AspNet.OData 7.0.0 then the namespace you are looking for is:

using Microsoft.AspNet.OData.Extensions;
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • 1
    This is like playing the game: "Find the Namespace" Thank you for pointing me to this namespace. Is there any documentation that I am overlooking that makes this clear? – Jim Aug 22 '18 at 18:41
  • I used ILSPY since I couldn't find anything decent when I looked for it.. – Amir Popovich Aug 23 '18 at 05:40
4

MapODataServiceRoute is extension method. So to use it a reference to its namespace is required. For me it was fixed by referencing:

using System.Web.Http.OData.Extensions;
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • 1
    And I'm guessing because it's an extension method, the right-click resolve reference doesn't know how to give that suggestion? – Watson Jul 03 '15 at 22:39
  • *System.Web.Http.OData.Extensions* worked for me where *System.Web.OData.Extensions* did not. Was the namespace switched between versions? – user3953989 Oct 28 '16 at 15:02
0

I fixed this by opening the package manager console, setting the default project to the project that gave the error message, and then:

Install-Package Microsoft.AspNet.WebApi.OData
johanv
  • 962
  • 8
  • 11