Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modern WebAPIS with ASP.NET Core

Modern WebAPIS with ASP.NET Core

Slides for my talk at the DWX 2018 in Nürnberg "Modern WebAPIs with ASP.NET Core"

Fabian Gosebrink

June 25, 2018
Tweet

More Decks by Fabian Gosebrink

Other Decks in Technology

Transcript

  1. Program.cs 1. public class Program 2. { 3. public static

    void Main(string[] args) 4. { 5. CreateWebHostBuilder(args).Build().Run(); 6. } 7. 8. public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 9. WebHost.CreateDefaultBuilder(args) 10. .UseStartup<Startup>(); 11. }
  2. public static IWebHostBuilder CreateDefaultBuilder(string[] args) { var builder = new

    WebHostBuilder(); builder.UseKestrel(...) .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true); }) .ConfigureLogging((hostingContext, logging) => { logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); logging.AddConsole(); logging.AddDebug(); }) .ConfigureServices((hostingContext, services) => { //... }) .UseIISIntegration(); return builder; }
  3. Startup.cs 1. public class Startup 2. { 3. public Startup(IConfiguration

    configuration) 4. { 5. Configuration = configuration; 6. } 7. 8. public IConfiguration Configuration { get; } 9. 10. public void ConfigureServices(IServiceCollection services) 11. { 12. services.AddOptions(); 13. 14. services.AddCors(...); 15.
  4. 2xx

  5. 4xx

  6. 5xx

  7. The Controller 1. [Route("api/[controller]")] 2. public class FoodController : Controller

    3. { 4. private readonly IFoodRepository _foodRepository; 5. 6. public FoodController(IFoodRepository foodRepository) 7. { 8. _foodRepository = foodRepository; 9. } 10. 11. [HttpGet] 12. public ActionResult<List<FoodItemDto>> Get() 13. { 14. ICollection<FoodItem> foodItems = _foodRepository.GetAll(); 15. IEnumerable<FoodItemDto> dtos = foodItems 16. .Select(x => Mapper.Map<FoodItemDto>(x)); 17. 18. return Ok(dtos);
  8. { "departmentId": 10, "departmentName": "Administration", "locationId": 1700, "managerId": 200, "links":

    [ { "href": "10/employees", "rel": "employees", "type" : "GET" } ] }