繁体   English   中英

无法将值从angular服务发布到asp.net core angular中的Web API

[英]Unable to post values from angular service to Web API in asp.net core angular

我正在研究asp.net core(2.1)角度项目,这里我将参数从角度服务传递到Web API,但没有在API中获得这些值。

下面是我的代码,当我为Web API使用单独的项目时,它运行良好。 现在,由于我在asp.net核心项目本身中创建了一个控制器,因此无法在API参数列表中获取发布的值。

我是否需要添加/更改一些设置以从角度服务到API调用获取值?

角度服务:

getSortedPagedResults(filters): Observable<any> {
        debugger;
        return this._http.post(this.myAppUrl + 'api/Employee/EmployeesServerSide', filters);  
      }

API:

[HttpPost]
    [Route("api/Employee/EmployeesServerSide")]
    public IEnumerable<Users> EmployeesServerSide(Filters filters)
    {
        return objemployee.GetUsersServerSide(filters);
    }

在此处输入图片说明 在此处输入图片说明

启动课程:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });        
            services.Configure<CookiePolicyOptions>(options =>
            {                   
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
            {
                builder
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    //.WithOrigins("http://localhost:4200")
                    .AllowCredentials();
            }));    
            services.AddSignalR();
            services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
            });
        }  
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseCors("CorsPolicy");                
            app.UseSignalR(routes =>
            {
                routes.MapHub<NotifyHub>("/notify");
            });    
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });    
            app.UseSpa(spa =>
            {                    
                spa.Options.SourcePath = "ClientApp";    
                if (env.IsDevelopment())
                {
                    spa.Options.StartupTimeout = new TimeSpan(0, 0, 100);
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }

在API方面,使用FromBody

public IEnumerable<Users> EmployeesServerSide([FromBody] Filters filters)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM