Dotnet api linux: Difference between revisions
Jump to navigation
Jump to search
Line 73: | Line 73: | ||
We also need to ensure that we configure what type of media we support. In ASP .net this can be done by setting the setupAction. This will stop the default of json being returned. | We also need to ensure that we configure what type of media we support. In ASP .net this can be done by setting the setupAction. This will stop the default of json being returned. | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="c#"> | ||
public void ConfigureServices(IServiceCollection services) | public void ConfigureServices(IServiceCollection services) | ||
Line 81: | Line 81: | ||
}).AddXmlDataContractSerializerFormatters(); | }).AddXmlDataContractSerializerFormatters(); | ||
... | ... | ||
</syntaxhighlight> | |||
= Getting Resources = | |||
In the example they used automapper. This allow the mapping of Entities to DTO (Data Transfer Objects) | |||
<syntaxhighlight lang="bash"> | |||
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection | |||
</syntaxhighlight> | |||
And configure the service in the code | |||
<syntaxhighlight lang="c#"> | |||
public void ConfigureServices(IServiceCollection services) | |||
{ | |||
... | |||
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); | |||
... | |||
</syntaxhighlight> | |||
Create a profile class for the from and to mappings | |||
<syntaxhighlight lang="c#"> | |||
public class AuthorsProfile : Profile | |||
{ | |||
public AuthorsProfile() | |||
{ | |||
CreateMap<Entities.Author, Models.AuthorDto>() | |||
.ForMember( | |||
dest => dest.Name, | |||
opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}")) | |||
.ForMember( | |||
dest => dest.Age, | |||
opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge())); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 03:24, 29 July 2020
Set up with VS Code
Create Project
To create a webapi using .net core 3.1 simply type
dotnet new webapi
Restore Nuget Packages
dotnet add package Microsoft.EntityFrameworkCore
Install Migration Tool
Exercise called for using Add-Migration on windows. In Linux this translates to
# Install Tool
dotnet add package Microsoft.EntityFrameworkCore.Tools.Dotnet
# Install dotnet-ef
dotnet tool install --global dotnet-ef
# Run Migration Creation
dotnet ef migrations add InitialCreate
Install SQL Server
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
# Add to /etc/apt/source.list
# deb [arch=amd64,arm64,armhf] https://packages.microsoft.com/ubuntu/18.04/mssql-server-2019 bionic main
sudo apt-get update
sudo apt-get install -y mssql-server
sudo /opt/mssql/bin/mssql-conf setup
# Show working
systemctl status mssql-server --no-pager
Create user
CREATE DATABASE test;
GO
CREATE LOGIN test with PASSWORD = 'guess!';
GO
EXEC master..sp_addsrvrolemember @loginame = N'test', @rolename = N'dbcreator'
GO
Running Query
List tables in DB
select schema_name(t.schema_id) as schema_name,
t.name as table_name,
t.create_date,
t.modify_date
from sys.tables t
order by schema_name,
table_name;
sqlcmd -S localhost -U test -d CourseLibraryDB -Q list_tables.sql
Structuring and Implementing
Interacting with Resources through HTTP Methods
Below is a table which shows the methods and how they work with the Authors and Courses demo app along with suggested naming. Note the use of nouns
Content Negotiation
We also need to ensure that we configure what type of media we support. In ASP .net this can be done by setting the setupAction. This will stop the default of json being returned.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(setupAction => {
setupAction.ReturnHttpNotAcceptable = true;
}).AddXmlDataContractSerializerFormatters();
...
Getting Resources
In the example they used automapper. This allow the mapping of Entities to DTO (Data Transfer Objects)
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
And configure the service in the code
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
...
Create a profile class for the from and to mappings
public class AuthorsProfile : Profile
{
public AuthorsProfile()
{
CreateMap<Entities.Author, Models.AuthorDto>()
.ForMember(
dest => dest.Name,
opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
.ForMember(
dest => dest.Age,
opt => opt.MapFrom(src => src.DateOfBirth.GetCurrentAge()));
}
}
}