Sunil Kumar
Sunil Kumar

Reputation: 949

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

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

enter image description here enter image description here

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

Upvotes: 0

Views: 681

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

On your API side use FromBody

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

}

Upvotes: 1

Related Questions