SERVICING STATIC WEBSITES USING WEB SERVERS
The previous application can be extended to serve static web pages. In the previous example, on request entered in the address field in the web browser:
http://localhost:5000
process ie. the web application responded to the request by returning a welcome message and system time. The application can be expanded by adding static web pages within a new folder, e.g. wwwroot located in the root folder of that application. These pages are actually html pages e.g.
Contact.html, Index.html, News.html, Students.html
Now the request to call the web application (Http web server), ie. The URLs that should return the content of these pages should be, for example:
Contact.html, Index.html, News.html, Students.html
Now the request to call the web application (Http web server), ie. The URLs that should return the content of these pages should be, for example:
http://localhost:5000/Students.html
ili
http://localhost:5000/Contact.html
In order for the web server to return these pages at the client's request, it is necessary to add the following two lines of code in the Startup.cs file within the Configure method:
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseStaticFiles();
In this way, the web server is instructed to use the default location for static files, which is wwwroot.
The UseStaticFile () command is required to use static files: html, javascript, css, etc.
See the following video on how to create such a web application using the VS Code tool:
See the following video on how to create such a web application using the VS Code tool:
Web services in ASP.NET CORE
The previous examples describe how to create a simple web server that returns a text or html page on demand by a web browser, but sometimes it is necessary to create a web service.
A web service is an application that, when requested by a browser, sends data or a set of data, not an html page.
An example of a simple web service is a service that returns the current time from a server.
If a search keyword such as "Tesla" is typed into Google's web browser, Google's web service will most likely perform a function that passes the word "Tesla" as a parameter and finds similar words, e.g. tesla, teslić, tesla transformer etc. and which are returned in response to a request to the web browser.
Creating the application skeleton is done using the command in the command prompt:
A web service is an application that, when requested by a browser, sends data or a set of data, not an html page.
An example of a simple web service is a service that returns the current time from a server.
If a search keyword such as "Tesla" is typed into Google's web browser, Google's web service will most likely perform a function that passes the word "Tesla" as a parameter and finds similar words, e.g. tesla, teslić, tesla transformer etc. and which are returned in response to a request to the web browser.
Creating the application skeleton is done using the command in the command prompt:
dotnet new web
A web service that returns system time on the server was actually created in one of the previous examples, by adding the MapGet () command of a class object that implements the IEndpointRouteBuilder interface and maps the url call:
http://localhost:5000/time
to a web service that displays system time, see below figure:
Using the: endpoint.MapGet () commands, other web services can be added, such as call service :
http://localhost:5000/day
return the day of the week, which can be seen in the following figure:
If we would now like to call a web service and pass it a parameter, e.g. by calling through a web browser using the following URL:
http://localhost:5000/hello?name=Mihajlo
then in the implementation of the web service we would have to extract the previously sent parameter name = Mihajlo which represents the name to be attached to the welcome message. The following figure shows the code that will allow this:
To extract the name, we first create a variable "name", which we will extract from the "context.Request" object using the Querry property, which is actually a set of parameters defined in the call URL after "?". The value of the "name" parameter is extracted from the set by access via the name of the Querry [name] parameter.
After runing the application:
After runing the application:
Previous
|< ASP.NET Core web application |
Next
Creating a sql web api service that reads data from a database >| |