BoldDesk®Customer service software with ticketing, live chat & omnichannel support, starting at $99/mo for unlimited agents. Try for free!
What is the best approach to add a web api as a project to the default ASP.NET Blazor solution? When using visual studio 2019 you no longer get the server and web projects when you select Blazor server on create new solution/project.
I found a way be browsing serveral blogs but it was mentioned that microsoft might dis-continue this.
Looking for the best way.
The reason I need to add an API to an ASP.NET Blazor Side project is to so the SyncFusion DataGrid can bind to it. See: https://blazor.syncfusion.com/documentation/datagrid/data-binding/#entity-framework
Here is what I am thinking of trying:
Use "options.EnableEndpointRouting" approach.
using Microsoft.AspNetCore.Mvc;
services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
app.UseMvcWithDefaultRoute();
Is this considered a future safe or supported way to do it?
Server Side Blazor Created By VS 2019 Wizard:
<SfGrid @ref="Grid" TValue="Order" AllowFiltering="true" Toolbar="@(new List<string> {"Add","Edit","Delete","Update","Cancel","Search" })" AllowSorting="true" AllowPaging="true">
<SfDataManager Url="api/Default" Adaptor="Adaptors.WebApiAdaptor"></SfDataManager>
<GridEvents OnActionFailure="Fail" TValue="Order"></GridEvents>
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true"></GridEditSettings>
<GridColumns>
<GridColumn Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="CustomerID" HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field="EmployeeID" HeaderText="ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
|
@Vignesh Natarajan
Thanks so much for your detailed response.
Syncfusion has AWESOME support!!!
---
The documentation that I had already reviewed
laid out that for the SF grid component to connect to entity framework, a web
API was recommended.
My question was really more towards.... If I am
using Blazor SERVER NOT Blazor Web Assembly,is there an easier way to
connect the SF grid to Entity Framework or a DAL without a Web API in the
middle.
For some generic fast start cases ( like most I
do) I skip the DAL and chose not to use Blazor Web Assembly because I don't
want the work involved in writing and maintaining Web APIs.
In the Blazor Server setup, can we bind direct
to entity framework in someway?
I really like DALs and WebAPIs but if you have tons of
little tables to get out, you need to save the time in MVP.
<SfGrid @ref="Grid" DataSource="@GridData" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })" AllowFiltering="true" AllowSorting="true" AllowPaging="true">
<GridEditSettings AllowAdding="true" AllowDeleting="true" AllowEditing="true" Mode="EditMode.Dialog"></GridEditSettings>
<GridEvents OnActionBegin="OnBegin" OnActionComplete="OnComplete" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" Visible="false" IsIdentity="true" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
SfGrid<Order> Grid { get; set; }
public IEnumerable<Order> GridData { get; set; }
protected override void OnInitialized()
{
GridData = OrderData.GetAllOrders().ToList();
}
public void OnComplete(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save || Args.RequestType == Syncfusion.Blazor.Grids.Action.Cancel)
{
// fetch updated data from service and bind to grid datasource property
GridData = OrderData.GetAllOrders().ToList();
}
}
public void OnBegin(ActionEventArgs<Order> Args)
{
if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save) // update the changes in Actionbegine event
{
if (Args.Action == "Add")
{
//insert the record into database
OrderData.AddOrder(Args.Data);
}
else
{
//update the existing record
OrderData.UpdateOrder(Args.Data);
}
}
else if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Delete)
{
// delete the record from your database
OrderData.DeleteOrder(Args.Data.OrderID);
}
}
|