简体   繁体   English

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

[英]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. 我正在研究asp.net core(2.1)角度项目,这里我将参数从角度服务传递到Web API,但没有在API中获得这些值。

Below is my code, it was working fine when I was using a separate project for Web API. 下面是我的代码,当我为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. 现在,由于我在asp.net核心项目本身中创建了一个控制器,因此无法在API参数列表中获取发布的值。

Do I need to add/change some settings to get values from angular service to API call? 我是否需要添加/更改一些设置以从角度服务到API调用获取值?

Angular Service : 角度服务:

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

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

On your API side use FromBody 在API方面,使用FromBody

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

}

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

相关问题 无法从asp.net核心api获取数据到angular 6 - Unable to fetch data from asp.net core api into angular 6 Angular POST从ASP.NET Core Web API返回500错误 - Angular POST returns 500 error from ASP.NET Core Web API 尝试将文件从 Angular 6 发布到 ASP.NET Core Web Api 时出错 - Error on try to post file from Angular 6 to ASP.NET Core Web Api 无法使用 NgModel 将带有附件的 SharePoint 列表项从 Angular 添加到 ASP.NET Core Web API - Unable to add SharePoint list item with attachment from Angular to ASP.NET Core Web API using NgModel 从Angular 2前端到Asp.Net Core Web API的JSON数据发布-丢失值 - JSON data posts from Angular 2 front end to Asp.Net Core web api - loses values 如何从 ASP.NET 内核 Web API 接收文件并在不同的 ZC31C335EF37283C451B18BA0DD31 服务选项卡中显示 - How to receive a file from ASP.NET Core Web API from Angular service and display in a different chrome tab IIS 上的 Angular Post 到 Asp.Net(非核心)Web Api 错误 415 - Angular Post to Asp.Net (not Core) Web Api Error 415 on IIS 使用 Angular 9 在 Asp.Net Core Web API 中发布数据(发布请求)时出错 - Error while posting data (Post request) in Asp.Net Core Web API with Angular 9 用角度5分析asp.net core 2(web api) - Profiling asp.net core 2 (web api) with angular 5 ASP.NET Core Web API + Angular 2授权和身份验证 - ASP.NET Core Web API + Angular 2 Authorization and Authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM