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

…衆ロ難τιáo~ 提交于 2021-02-08 09:55:04

问题


I am working on asp.net core(2.1) angular project, here I am passing parameters from angular service to web API but I am not getting those values in API.

Below is my code, it was working fine when I was using a separate project for Web API. Now as I have created a controller inside the asp.net core project itself I am unable to get posted values in API parameter list.

Do I need to add/change some settings to get values from angular service to API call?

Angular Service :

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);
    }

Startup Class :

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");
                }
            });
        }

回答1:


On your API side use FromBody

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

}


来源:https://stackoverflow.com/questions/51279989/unable-to-post-values-from-angular-service-to-web-api-in-asp-net-core-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!