无法从odata获取数据

时间:2019-02-20 08:57:53

标签: asp.net asp.net-core odata

以前odata可以正常工作,但是当我将模型名称从taContenants更改为TA_CONTENANTS时,我将模型名称更改为蛇形(我需要进行这些更改,以将正确的名称映射到EF数据库的数据库字段第一种方法)

当我添加此行

            //services.AddODataQueryFilter();

一无所有

现在我在http://localhost:57746/api/odata/TA_CONTENANTS上遇到此错误  

Cannot GET /api/odata/TA_CONTENANTS

这是config

namespace KmCore
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddEnvironmentVariables().Build();

            configuration = builder;
            Configuration = configuration;
            StaticConfig = configuration;
            C_Interface_Meta.IntialiserBdd();
        }

        public IFileProvider FileProvider { get; }

        public static IConfiguration StaticConfig { get; private set; }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SI_KM")));
            services.AddDbContext<NSG_DBContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("NSG_DB")));

            //DEvexpress
            services.AddDevExpressControls(settings => settings.Resources = ResourcesType.ThirdParty | ResourcesType.DevExtreme);

            //Devextreme
            services
              .AddCors(options =>
              {
                  options.AddPolicy("AllowAnyOrigin", builder =>
                  {
                      builder.AllowAnyOrigin();
                  });
              });
            services
            .AddMvc()
            // ... other settings of the MVC service ...
            .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
            //services.AddMvc().AddWebApiConventions(); //Add WebApi
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = Configuration["Jwt:Issuer"],
                    ValidAudience = Configuration["Jwt:Issuer"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
                };
            });
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddMvc().AddDefaultReportingControllers();
            services.ConfigureReportingServices(configurator =>
            {
                configurator.ConfigureReportDesigner(designerConfigurator =>
                {
                    designerConfigurator.RegisterDataSourceWizardConfigFileConnectionStringsProvider();
                });
            });

            services.AddOData();
            //services.AddODataQueryFilter();
            //services.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            DashboardConfigurator.PassCredentials = true;
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the Angular files will be served from this directory

            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            var reportDirectory = Path.Combine(env.ContentRootPath, "Reports");
            DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension.RegisterExtensionGlobal(
                new KmReportStorageWebExtension("DefaultConnection", reportDirectory));
            app.UseCors("AllowAnyOrigin");
            if (env.IsDevelopment())
            {
                //app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseDevExpressControls();

            app.UseAuthentication();
            app.UseMvc(b =>
            {
                b.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
                b.MapODataServiceRoute("odata", "api/odata", GetEdmModel(app.ApplicationServices));
            });
            app.UseMvc(routes =>
            {
                routes.MapDashboardRoute("api/dashboard");
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core, see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";
                spa.Options.StartupTimeout = new TimeSpan(0, 0, 360);
                //spa.UseAngularCliServer(npmScript: "start");
                //spa.UseProxyToSpaDevelopmentServer("http://localhost:58135/");
                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "debug");
                }
            });
        }

        private static IEdmModel GetEdmModel(IServiceProvider serviceProvider)
        {
            ODataModelBuilder builder = new ODataConventionModelBuilder(serviceProvider);
            ODataConventionModelBuilder builder1 = new ODataConventionModelBuilder();
            builder.EntitySet<MO_UTILISATEURS>("MO_UTILISATEURS");
            builder.EntitySet<TA_CONTENANTS>("TA_CONTENANTS");
            builder.EntitySet<EE_MESURES>("EE_MESURES");
            builder.EntitySet<MO_UTILISATEURS_MENU>("MO_UTILISATEURS_MENU");
            builder.EntitySet<V_KM_MO_MENU>("V_KM_MO_MENU");
            return builder.GetEdmModel();
        }

    }
}

有人可以帮我吗?

0 个答案:

没有答案