मैं अपने एएसपी.नेट वेब एपीआई में ओडाटा रूटिंग को लागू करने की कोशिश कर रहा हूं। मार्गदर्शन के लिए, मैंने इस ट्यूटोरियल को देखा: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint।एएसपी.नेट वेब एपीआई में ओडाटा वी 4.0 रूटिंग को सक्षम करने में समस्या 2.2
हालांकि, मुझे MapODataServiceRoute() फ़ंक्शन में एक त्रुटि संदेश प्राप्त करना जारी रहता है। जाहिर है, फ़ंक्शन एक Microsoft.OData.Edm.IEdmModel की अपेक्षा कर रहा है, और मेरे निर्माता का GetEdmModel() फ़ंक्शन केवल Microsoft.Data.Edm.IEdmModel देता है।
मैंने कुछ शोध ऑनलाइन किया। Microsoft.Data.Edm ओडाटा के पुराने संस्करण के लिए एक लाइब्रेरी है। Microsoft.OData.Edm OData v4.0 के लिए है, इसलिए मैंने WebApiConfig.cs फ़ाइल में Microsoft.Data.Edm पर टिप्पणी की। मेरा कोड यहाँ है।
Error 1 Cannot implicitly convert type 'Microsoft.Data.Edm.IEdmModel' to 'Microsoft.OData.Edm.IEdmModel'. An explicit conversion exists (are you missing a cast?)
वहाँ एक Microsoft.OData.Edm.IEdmModel प्राप्त करने के लिए एक साफ रास्ता है:
using MyApp.Models;
// using Microsoft.Data.Edm;
using Microsoft.OData.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;
namespace MyAppAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Enable attribute routing
config.MapHttpAttributeRoutes();
// Enable OData routing
config.MapODataServiceRoute(
routeName: "MyApp",
routePrefix: "odata",
model: GetEdmModel());
// Conventional routing
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
// Trying to get most browsers (i.e. Google Chrome) to return JSON
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
// Configure modesl to use Odata
public static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ModelA>("ModelA");
builder.EntitySet<ModelB>("ModelB");
return builder.GetEdmModel();
}
}
}
हालांकि, मैं अब भी एक त्रुटि संदेश मिल रहा है? या मुझे सिर्फ एक कलाकार करना है?