Integrate MVC Web Applications with Business Central SOAP
Integrate MVC Web Applications with Business Central SOAP
About MVC (Model View Controller)
The Model-View-Controller (MVC) architectural pattern divides the apps into three main components: Model, View and Controller. The MVC pattern lets you create simpler apps to update and test against traditional monolithic apps.
• Models: classes that represent the data of the app. Model classes use the validation logic to apply business rules for such data. Typically, model objects retrieve and store the model state in a database.
• Views: components that display the user interface of the app; typically this user interface presents the model data.
• Controller: classes that handle browser requests. They retrieve model data and call view models that return a response. In an MVC application, the view presents only the information; the controller manages and responds to user input and interaction actions.
MVC
Source https://docs.microsoft.com/it-it/aspnet/core/tutorials/first-mvc-app/?view=aspnetcore-2.2
About Razor Pages
Razor Pages is a new feature of ASP.NET Core MVC that simplifies and makes the writing of code focused on pages more productive.
Razor is a markup syntax for embedding server-based code in Web pages. The Razor syntax consists of Razor, C # and HTML markup. Files containing Razor typically have the cshtml extension. Razor supports C# and uses the @ symbol for the transition from HTML to C#. Razor evaluates C # expressions and renders them in the HTML output.
https://docs.microsoft.com/it-it/aspnet/core/mvc/views/razor?view=aspnetcore-2.2
Requirements
- Visual Studio 2019 with the ASP.NET and web development workload
- .NET Core SDK 2.2 or later
Comparing SOAP and OData Web Services
Differences between SOAP and Odata web services for Dynamics 365 Business Central-
SOAP Web Services
SOAP web services allow full flexibility for building operation-centric services. The most common type of messaging pattern in SOAP is the Remote Procedure Call (RPC), where one network node (the client) sends a request message to another node (the server), and the server sends a response message to the client.
OData Web Services
The OData standard is well suited for web service applications that require a uniform, flexible, general-purpose interface for exposing create retrieve update delete (CRUD) operations on a tabular data model to clients. OData supports Representational State Transfer (REST)-based data services.
Example of SOAP
http://localhost:7048/BC130/WS/Services
Example of OData
http://localhost:7048/BC130/OData/Company(‘CRONUS-International-Ltd.’)/Customer?Tenant=Tenant1
#1 – NEW STYLE APPS: Introduction to ASP.NET Core MVC and Razor Pages
Create a New Project Web ASP.NET Core
Create a new MVC App
MVC Solution (MVC Projects Sections – Models, Controllers, Views)
MVC Web Application will be deployed on IIS Express – example
#2 – EXAMPLE OF SIMPLE SOLUTION WITH MVC AND BUSINESS CENTRAL SOAP
Scenario1: Read Companies names form Business Central Web Services List and show results on Web Page.
Scenario2: Read Item List and Insert a New Item using WS (Codeunit and Card Page).
“Services List” is exposed as Web Service (SOAP API), the same is for “ItemCard” & “ItemList”
Services Page
https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/Services
Item Pages
https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/COMPANYNAME/Page/ItemList
https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/COMPANYNAME/Page/ItemCard
Microsoft Demo Source https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/use-systemservice-to-find-companies
Create a New Project “DemoBCWebApp”
Add a Web Reference to Services.
Configure the WFC Web Reference (in this example the System Services WS show Companies, the same for other Web Services), assign a namespace ex: BCSOAP
MVC Application Structure: “MODELS, CONTROLLERS, VIEWS”
CONTROLLERS
The Core of the Web Application
CreateItem
On HomeController.cs you can recall the “CreateItem” function exposed by Web Service
GetItemList
You can define an “ItemList” function to retrieve data from the ItemList exposed Web Service.
You need to use these include:
- System.Web, System.Web.Mvc and others, all the exposed Web Service one by one
- You have to define User, Password for connection to Web Services
Define the URL Connection, User and Password
URL Connection Sample and Service Login example
systemService.Url = “https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/SystemService?tenant=<tenant>=FIN”
NetworkCredential nc = new NetworkCredential(“user”, “password”)
About Security
** User\Password: is possibile to Crypt\Decrypt password or masquerade the code with Dotfuscator (many post available about these issues)
** User Login Page: you can activate a separate login page – Login Wrapper (you can use Google Sites as Master Browser, read my post about Power BI reports publications)
Simple User Login Form example in ASP.Net
https://www.aspsnippets.com/Articles/Simple-User-Login-Form-example-in-ASPNet.aspx
C#
using System.Web.Security;
User Login Page: example
Dotfuscator
Integrated in .NET Application
MVC Application Main Objects
- Global.asax, _Layout.cshtml and AppConfig
_Layout.cshtml
Web Config
MODELS
VIEWS
View and Display Data, in this case Companies names will be displayed in the Web Page.
Index.cshtml the core code to display data
@foreach (var com in Model)
{
…. Loop and write @com
}
Call mapped Web Services: Call the WSCdu codeunit to insert data.
RUNNING TESTS
Is a Web Application, style Is a lot different from classic desktop apps.
Login Page
Home Page
“Insert” and Show “added data” after insert
- From Button “Insert” you can call the “CreateItem” Function from exposed Codeunit to insert a new item or write directly to another Web Services exposed (ex: ItemCard)
Codeunit WSCdu > “CreateItem” Function
List of Exposed Web Services
https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/COMPANYNAME/Page/ItemList
https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/COMPANYNAME/Page/ItemCard
https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/ COMPANYNAME /Codeunit/WSCdu_Functions
Codeunit WSCdu > CreateItem Function
Web Services references
Insert New Item from Codeunit Web Service o from ItemCard Web Service (two different methods)
“Inserted Item” message: “Success” or “Failure”
Success message
Failure message
#3 – OLD STYLE APP – CLASSIC C# Console Application
You can achieve the same result using a classic desktop application.
ADD a Web Reference – “SystemService” SOAP Web Service
CONSOLE APPLICATION C# CONNECTION CODE
systemService.Url = “https://api.businesscentral.dynamics.com/v1.0/<TenantID>/WS/SystemService?tenant=<tenant>=FIN”
NetworkCredential nc = new NetworkCredential(“user”, “password”)
RUNNING TEST > List of ALL companies will be displayed in Console Mode
Hello Roberto,
I’ve found this blog searching about info to create a web Api for a oData webService. The process will the the same in this case as SOAP?
Thank you very much
how can we delete and edit records from Item list through web to business central?
Detailed one roberto. Will include this article in our mvc tutorial.
Detailed one roberto. Will include this article in our mvc tutorial.