How to display the data on the grid view in which data will come from the asp .net web API

Hello!
I am working on the angular 9 project in which I want to display the data on the grid view in which data will come from the asp .net web API so I want the solution for it with CORS configuration

3 Replies 1 reply marked as answer

TS Thiyagu Subramani Syncfusion Team September 15, 2020 02:11 AM UTC

Hi Ishan, 

Thanks for contacting Syncfusion forum. 

Currently we are checking angular 9 with asp.net web API at our end but we are facing some issues. So we will update further details on 16th September 2020. Until that time we appreciate your patience. 

Regards, 
Thiyagu S 



TS Thiyagu Subramani Syncfusion Team September 17, 2020 02:41 AM UTC

Hi Ishan, 

Sorry for the inconvenienced Caused. 

We are facing complexity to validate your requirement at our end and we will update further details on today(17th Sep 2020). Until that time we appreciate your patience. 

Regards, 
Thiyagu S 



TS Thiyagu Subramani Syncfusion Team September 17, 2020 12:21 PM UTC

Hi Ishan, 

Thanks for your patience. 

Based on your suggestion we have prepared a sample with the help of our Angular with ASP.Net getting started section. To enable CORS configuration in your application we suggest you to enable the cross-origin request in the startup.cs file as mentioned in the below code example. Please refer it.
 
 
[startup.cs]   
public void ConfigureServices(IServiceCollection services) 
        { 
. . . . 
            services.AddCors(options => 
            { 
                options.AddPolicy("EnableCORS", builder => 
                { 
                    builder.AllowAnyOrigin().AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials().Build(); 
               }); 
            }); 
             
            . . . .. .   
        } 
 
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
        public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
        { 
            app.UseCors("EnableCORS"); 
. . .. .  . 
            });   
   
Cross configuration for aspnetcore version as 3  : https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1 

By default in ASP.NET Core the JSON results are returned in camelCase format, so the grid field names are also changed in camelCase and grid will be render without data. The mentioned issue is reproduced due to the SerializerSettings. So, we suggest you to add the below code in your startup.cs file to overcome the problem. 

public void ConfigureServices(IServiceCollection services) 
        { 
            services.AddMvc().AddJsonOptions(x => 
            { 
                x.SerializerSettings.ContractResolver = null; 
            }); 
            . . . .  
        } 


In this sample we have render grid with web API request using below code snippets. 

<ejs-grid #grid [dataSource]='data' allowPaging="true"> 
  <e-columns> 
    <e-column field='OrderID' headerText='Order ID' isPrimaryKey=true width='150'></e-column> 
    <e-column field='CustomerID' headerText='Customer Name' width='150'></e-column> 
    <e-column field='ShipCity' headerText='ShipCity' width='150' textAlign='Right'></e-column> 
  </e-columns> 
</ejs-grid> 



export class Pivot_Table1_Component { 
   public data: any; 
    @ViewChild('grid') 
    public grid: GridComponent; 
    ngOnInit(): void { 
        this.data = new DataManager({ 
            url: 'http://localhost:55055/api/Orders/',   // Send hosted link 
            adaptor: new WebApiAdaptor, 
            crossDomain: true 
        }); 
        
    } 



To run our sample please follow the below steps. 

1. Run WebApplciation5 in visual studio and set this hosted link to Webapi Data Manger’s URL like above code snippet. 

 

2. Run “npm install” command in ClientApp root folder. 

3. Run angular project (ClientApp) using “ng serve” command. 

 

Note :  if you are facing the serve command requires to be run in an angular project, but a project definition could not be found angular while running the ng serve command please follow below reference links. 



Please get back to us, if you need any further assistance. 

Regards, 
Thiyagu S. 


Marked as answer
Loader.
Up arrow icon