簡體   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