├── AdvWorks.WebAPI ├── AdvWorks.WebAPI.csproj ├── Controllers │ ├── ProductController.cs │ └── ValuesController.cs ├── Models │ ├── ProductDTO.cs │ └── ProductDetailsDTO.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Services │ ├── IProductRepository.cs │ └── ProductRepository.cs ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── AdvWorksAPI.DataAccess ├── AdvWorksAPI.DataAccess.csproj ├── AdvWorksContext.cs ├── App.config ├── Product.cs ├── Properties │ └── AssemblyInfo.cs └── packages.config ├── AdvWrksDapper ├── AdvWrksDapper.csproj ├── Controllers │ ├── DepartmentController.cs │ └── ValuesController.cs ├── Models │ ├── AdvWorksConfig.cs │ └── Department.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Repository │ ├── DepartmentRepository.cs │ └── IDepartmentRepository.cs ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── AsyncWalletDemo ├── AsyncWalletDemo.csproj ├── Controllers │ ├── ValuesController.cs │ └── WalletController.cs ├── Migrations │ ├── 20170301174411_InitialSetup.Designer.cs │ ├── 20170301174411_InitialSetup.cs │ └── WalletContextModelSnapshot.cs ├── ModelsContext │ ├── WalletContext.cs │ └── Wallets.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── BasicRoutes ├── BasicRoutes.csproj ├── DomainConstraint.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md └── Startup.cs ├── BudegetIdentityDemo ├── BudegetIdentityDemo.csproj ├── Controllers │ ├── AuthController.cs │ ├── BudgetCategoryController.cs │ ├── CredentialModel.cs │ ├── TwoFactorAuthController.cs │ ├── ValuesController.cs │ └── VerifyCodeViewModel.cs ├── IdentityDbSeeder.cs ├── Migrations │ ├── 20170504114713_initial.Designer.cs │ ├── 20170504114713_initial.cs │ └── IdentityDbContextModelSnapshot.cs ├── Models │ └── BudgetCategory.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── CustomRouteConstraints ├── Controllers │ ├── EmployeeController.cs │ └── ValuesController.cs ├── CustomRouteConstraints.csproj ├── DomainConstraint.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── LICENSE ├── LinkGeneration ├── Controllers │ ├── TodoController.cs │ └── ValuesController.cs ├── LinkGeneration.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── MasteringAspNetWebApi.postman_collection.json ├── MasteringAspNetWebApiSln.sln ├── MyFirstCoreAPI ├── Controllers │ └── ValuesController.cs ├── MyFirstCoreAPI.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── MyFirstCoreApiRoutes ├── Controllers │ ├── PacktController.cs │ ├── PacktsController.cs │ └── ValuesController.cs ├── MyFirstCoreApiRoutes.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── MyWallet ├── Controllers │ ├── NewWalletController.cs │ ├── ValuesController.cs │ └── WalletController.cs ├── ErrorHandlers │ ├── WebAPIError.cs │ ├── WebApiException.cs │ └── WebApiExceptionFilter.cs ├── ModelsContexts │ ├── DailyExpense.cs │ └── ExpenseContext.cs ├── MyWallet.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── nlog.config ├── PacktContacts ├── .dockerignore ├── Controllers │ ├── AuthController.cs │ ├── ContactsController.cs │ └── ValuesController.cs ├── Dockerfile ├── Model │ ├── AppUser.cs │ ├── Contacts.cs │ ├── CredentialsModel.cs │ ├── PacktContactsContext.cs │ └── TestData.cs ├── PacktContacts.csproj ├── PacktHeaderValidator.cs ├── Program.cs ├── Properties │ ├── PublishProfiles │ │ └── FolderProfile.pubxml │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── PacktContactsCore ├── Context │ └── ContactsContext.cs ├── Controllers │ ├── ContactsController.cs │ └── ValuesController.cs ├── Migrations │ ├── 20170128170832_init.Designer.cs │ ├── 20170128170832_init.cs │ └── ContactsContextModelSnapshot.cs ├── Model │ └── Contacts.cs ├── PacktContactsCore.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json └── appsettings.json ├── PacktContactsJsUI ├── App_Start │ └── ReactConfig.cs ├── Content │ ├── bootstrap-theme.css │ ├── bootstrap-theme.css.map │ ├── bootstrap-theme.min.css │ ├── bootstrap-theme.min.css.map │ ├── bootstrap.css │ ├── bootstrap.css.map │ ├── bootstrap.min.css │ └── bootstrap.min.css.map ├── JavascriptUI.html ├── JqueryUI.html ├── PacktContactsJsUI.csproj ├── Properties │ └── AssemblyInfo.cs ├── README.md ├── ReactUI.html ├── Scripts │ ├── JScallservice.js │ ├── JqueryService.js │ ├── PacktContacts.jsx │ ├── bootbox.min.js │ ├── bootstrap.js │ ├── bootstrap.min.js │ ├── jquery-3.1.1.intellisense.js │ ├── jquery-3.1.1.js │ ├── jquery-3.1.1.min.js │ ├── jquery-3.1.1.min.map │ ├── jquery-3.1.1.slim.js │ ├── jquery-3.1.1.slim.min.js │ ├── jquery-3.1.1.slim.min.map │ └── react │ │ ├── react-dom-server.js │ │ ├── react-dom-server.min.js │ │ ├── react-dom.js │ │ ├── react-dom.min.js │ │ ├── react-with-addons.js │ │ ├── react-with-addons.min.js │ │ ├── react.js │ │ └── react.min.js ├── Views │ └── web.config ├── Web.Debug.config ├── Web.Release.config ├── Web.config ├── fonts │ ├── glyphicons-halflings-regular.eot │ ├── glyphicons-halflings-regular.svg │ ├── glyphicons-halflings-regular.ttf │ ├── glyphicons-halflings-regular.woff │ └── glyphicons-halflings-regular.woff2 ├── index.html └── packages.config ├── PersonalBudget ├── Controllers │ ├── AuthController.cs │ ├── BudgetCategoryController.cs │ └── ValuesController.cs ├── IdProtectorConverter.cs ├── Models │ ├── AppUser.cs │ ├── BudgetCategory.cs │ ├── BudgetCategoryDTO.cs │ ├── CredentialsModel.cs │ ├── PersonalBudgetContext.cs │ └── TestData.cs ├── PersonalBudget.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── StringConstants.cs ├── appsettings.Development.json └── appsettings.json ├── Puhoi └── Puhoi-master │ ├── .gitignore │ ├── Puhoi.Business │ ├── Interfaces │ │ └── IStoreManager.cs │ ├── Managers │ │ ├── ProductManager.cs │ │ └── StoreManager.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Puhoi.Business.csproj │ └── packages.config │ ├── Puhoi.Common │ ├── FluentValidationExtension.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Puhoi.Common.csproj │ └── packages.config │ ├── Puhoi.Data │ ├── DataStore.cs │ ├── IDataStore.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Puhoi.Data.csproj │ ├── Puhoi.DataModels │ ├── BaseDto.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Puhoi.DataModels.csproj │ └── StoreDto.cs │ ├── Puhoi.Models.Tests │ ├── GreenProductModel.cs │ ├── ProductModelShould.cs │ ├── ProductModelValidatorShould.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Puhoi.Models.Tests.csproj │ └── packages.config │ ├── Puhoi.Models │ ├── HttpModelResult.cs │ ├── Models │ │ ├── BaseModel.cs │ │ ├── ProductModel.cs │ │ └── StoreModel.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Puhoi.Models.csproj │ ├── Validators │ │ ├── BaseValidator.cs │ │ ├── ProductModelValidator.cs │ │ └── StoreModelValidator.cs │ └── packages.config │ ├── PuhoiAPI.Tests │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── PuhoiAPI.Tests.csproj │ ├── StoresControllerShould.cs │ └── packages.config │ ├── PuhoiApi.sln │ └── src │ ├── Puhoi.Common.Core │ ├── FluentValidationExtension.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Puhoi.Common.Core.csproj │ ├── Puhoi.Models.Core │ ├── HttpModelResult.cs │ ├── Models │ │ ├── BaseModel.cs │ │ ├── ProductModel.cs │ │ └── StoreModel.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Puhoi.Models.Core.csproj │ └── Validators │ │ ├── BaseValidator.cs │ │ ├── ProductModelValidator.cs │ │ └── StoreModelValidator.cs │ ├── Puhoi.Models.xUnit.Tests │ ├── GreenProductModel.cs │ ├── ProductModelShould.cs │ ├── ProductModelValidatorShould.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Puhoi.Models.xUnit.Tests.csproj │ ├── PuhoiAPI.xUnit.Tests │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── PuhoiAPI.xUnit.Tests.csproj │ ├── StoresControllerShould.cs │ └── appsettings.json │ └── PuhoiAPI │ ├── Attributes │ └── ModelValidationAttribute.cs │ ├── AutoMapperProfileConfiguration.cs │ ├── Controllers │ ├── StoresController.cs │ └── ValuesController.cs │ ├── Program.cs │ ├── Project_Readme.html │ ├── PuhoiAPI.csproj │ ├── Startup.cs │ ├── app.config │ ├── appsettings.json │ └── web.config ├── README.md ├── Specific instructions to run code files.pdf ├── basics-logging ├── Controllers │ └── ValuesController.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── basics-logging.csproj ├── builtin-middleware ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── builtin-middleware.csproj └── wwwroot │ └── index.html ├── compression-cache-demo ├── Controllers │ ├── ContactTypeController.cs │ ├── PersonController.cs │ └── ValuesController.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── compression-cache-demo.csproj ├── custom-middleware ├── Controllers │ └── ValuesController.cs ├── PacktHeaderValidator.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── custom-middleware.csproj ├── distributed-cache-demo ├── Controllers │ ├── CurrencyConverterController.cs │ ├── Rates.cs │ └── ValuesController.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── distributed-cache-demo.csproj ├── docker-compose.ci.build.yml ├── docker-compose.dcproj ├── docker-compose.override.yml ├── docker-compose.yml ├── filters-demo ├── CheckPubliserName.cs ├── Controllers │ ├── EmployeeController.cs │ ├── ProductsController.cs │ └── ValuesController.cs ├── PacktExceptionFilter.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── ZeroValueException.cs ├── appsettings.Development.json ├── appsettings.json └── filters-demo.csproj ├── httpmodule-to-middleware ├── AspxMiddleware.cs ├── Controllers │ └── ValuesController.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── httpmodule-to-middleware.csproj ├── middleware-basics ├── Program.cs ├── Properties │ └── launchSettings.json ├── README.md ├── Startup.cs └── middleware-basics.csproj ├── ngPacktContacts ├── .angular-cli.json ├── .editorconfig ├── .gitignore ├── README.md ├── e2e │ ├── app.e2e-spec.ts │ ├── app.po.ts │ └── tsconfig.e2e.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── protractor.conf.js ├── src │ ├── app │ │ ├── app.component.css │ │ ├── app.component.html │ │ ├── app.component.spec.ts │ │ ├── app.component.ts │ │ ├── app.module.ts │ │ ├── contacts.class.ts │ │ ├── contacts │ │ │ ├── contacts.component.css │ │ │ ├── contacts.component.html │ │ │ ├── contacts.component.spec.ts │ │ │ └── contacts.component.ts │ │ ├── credentials-model.ts │ │ ├── login │ │ │ ├── login.component.css │ │ │ ├── login.component.html │ │ │ ├── login.component.spec.ts │ │ │ └── login.component.ts │ │ ├── packt-apis.service.spec.ts │ │ ├── packt-apis.service.ts │ │ └── packt-routes │ │ │ └── packt-routes.module.ts │ ├── assets │ │ └── .gitkeep │ ├── environments │ │ ├── environment.prod.ts │ │ └── environment.ts │ ├── favicon.ico │ ├── index.html │ ├── main.ts │ ├── polyfills.ts │ ├── styles.css │ ├── test.ts │ ├── tsconfig.app.json │ ├── tsconfig.spec.json │ └── typings.d.ts ├── tsconfig.json └── tslint.json ├── packtcontactapp ├── .editorconfig ├── .gitignore ├── README.md ├── config.xml ├── ionic.config.json ├── package-lock.json ├── package.json ├── resources │ ├── android │ │ ├── icon │ │ │ ├── drawable-hdpi-icon.png │ │ │ ├── drawable-ldpi-icon.png │ │ │ ├── drawable-mdpi-icon.png │ │ │ ├── drawable-xhdpi-icon.png │ │ │ ├── drawable-xxhdpi-icon.png │ │ │ └── drawable-xxxhdpi-icon.png │ │ └── splash │ │ │ ├── drawable-land-hdpi-screen.png │ │ │ ├── drawable-land-ldpi-screen.png │ │ │ ├── drawable-land-mdpi-screen.png │ │ │ ├── drawable-land-xhdpi-screen.png │ │ │ ├── drawable-land-xxhdpi-screen.png │ │ │ ├── drawable-land-xxxhdpi-screen.png │ │ │ ├── drawable-port-hdpi-screen.png │ │ │ ├── drawable-port-ldpi-screen.png │ │ │ ├── drawable-port-mdpi-screen.png │ │ │ ├── drawable-port-xhdpi-screen.png │ │ │ ├── drawable-port-xxhdpi-screen.png │ │ │ └── drawable-port-xxxhdpi-screen.png │ ├── icon.png │ ├── ios │ │ ├── icon │ │ │ ├── icon-40.png │ │ │ ├── icon-40@2x.png │ │ │ ├── icon-40@3x.png │ │ │ ├── icon-50.png │ │ │ ├── icon-50@2x.png │ │ │ ├── icon-60.png │ │ │ ├── icon-60@2x.png │ │ │ ├── icon-60@3x.png │ │ │ ├── icon-72.png │ │ │ ├── icon-72@2x.png │ │ │ ├── icon-76.png │ │ │ ├── icon-76@2x.png │ │ │ ├── icon-83.5@2x.png │ │ │ ├── icon-small.png │ │ │ ├── icon-small@2x.png │ │ │ ├── icon-small@3x.png │ │ │ ├── icon.png │ │ │ └── icon@2x.png │ │ └── splash │ │ │ ├── Default-568h@2x~iphone.png │ │ │ ├── Default-667h.png │ │ │ ├── Default-736h.png │ │ │ ├── Default-Landscape-736h.png │ │ │ ├── Default-Landscape@2x~ipad.png │ │ │ ├── Default-Landscape@~ipadpro.png │ │ │ ├── Default-Landscape~ipad.png │ │ │ ├── Default-Portrait@2x~ipad.png │ │ │ ├── Default-Portrait@~ipadpro.png │ │ │ ├── Default-Portrait~ipad.png │ │ │ ├── Default@2x~iphone.png │ │ │ └── Default~iphone.png │ └── splash.png ├── src │ ├── app │ │ ├── app.component.ts │ │ ├── app.html │ │ ├── app.module.ts │ │ ├── app.scss │ │ └── main.ts │ ├── assets │ │ └── icon │ │ │ └── favicon.ico │ ├── index.html │ ├── manifest.json │ ├── pages │ │ ├── home │ │ │ ├── home.html │ │ │ ├── home.scss │ │ │ └── home.ts │ │ └── login │ │ │ ├── login.html │ │ │ ├── login.scss │ │ │ └── login.ts │ ├── providers │ │ ├── contacts.class.ts │ │ └── packt-service.ts │ ├── service-worker.js │ └── theme │ │ └── variables.scss ├── tsconfig.json ├── tslint.json └── typings │ └── cordova-typings.d.ts └── web ├── ReadMe.txt ├── WebDemoAPP1 ├── index.html └── package.json └── WebDemoAPP2 ├── index.html └── package.json /AdvWorks.WebAPI/AdvWorks.WebAPI.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net461 5 | aspnet-AdvWorks.WebAPI-C0769960-AF83-4748-80A1-B645B61A0052 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace AdvWorks.WebAPI.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/Models/ProductDTO.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace AdvWorks.WebAPI.Models 4 | { 5 | public class ProductDTO 6 | { 7 | public int ProductID { get; set; } 8 | 9 | [Required] 10 | [StringLength(50)] 11 | public string Name { get; set; } 12 | 13 | [Required] 14 | [StringLength(25)] 15 | public string ProductNumber { get; set; } 16 | 17 | [StringLength(15)] 18 | public string Color { get; set; } 19 | 20 | public short ReorderPoint { get; set; } 21 | 22 | public decimal StandardCost { get; set; } 23 | 24 | public decimal ListPrice { get; set; } 25 | 26 | public decimal? Weight { get; set; } 27 | 28 | public int DaysToManufacture { get; set; } 29 | } 30 | } -------------------------------------------------------------------------------- /AdvWorks.WebAPI/Models/ProductDetailsDTO.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace AdvWorks.WebAPI.Models 4 | { 5 | public class ProductDetailsDTO 6 | { 7 | 8 | [Required] 9 | [StringLength(50)] 10 | public string Name { get; set; } 11 | 12 | [Required] 13 | [StringLength(25)] 14 | public string ProductNumber { get; set; } 15 | 16 | [StringLength(15)] 17 | public string Color { get; set; } 18 | 19 | public short ReorderPoint { get; set; } 20 | 21 | public decimal StandardCost { get; set; } 22 | 23 | public decimal ListPrice { get; set; } 24 | 25 | public decimal? Weight { get; set; } 26 | 27 | public int DaysToManufacture { get; set; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace AdvWorks.WebAPI 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:64333/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AdvWorks.WebAPI": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:64334/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 9 - Integration With Database 12 | 13 | ### AdvWorks.WebAPI and AdvWorksAPI.DataAccess Project 14 | 15 | Demo code for below feature 16 | 17 | * Connecting, CRUD operation Adventure Works Database 18 | * Using Entity Framework 6 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/Services/IProductRepository.cs: -------------------------------------------------------------------------------- 1 | using AdvWorksAPI.DataAccess; 2 | using System.Collections.Generic; 3 | 4 | namespace AdvWorks.WebAPI.Services 5 | { 6 | public interface IProductRepository 7 | { 8 | IEnumerable GetProducts(); 9 | Product GetProduct(int productId); 10 | bool ProductExists(int productId); 11 | void AddProduct(Product proddetails); 12 | void DeleteProduct(Product proddetails); 13 | bool Save(); 14 | } 15 | } -------------------------------------------------------------------------------- /AdvWorks.WebAPI/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /AdvWorks.WebAPI/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "connectionStrings": { 16 | "AdvWorksDbConnection": "Server=.\\sqlexpress;initial catalog=AdventureWorks2014;Trusted_Connection=True;MultipleActiveResultSets=true" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /AdvWorksAPI.DataAccess/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /AdvWorksAPI.DataAccess/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /AdvWrksDapper/AdvWrksDapper.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-AdvWrksDapper-A130B19C-65C5-46E1-9DE9-40D3B6D9A3D9 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /AdvWrksDapper/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace AdvWrksDapper.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /AdvWrksDapper/Models/AdvWorksConfig.cs: -------------------------------------------------------------------------------- 1 | namespace AdvWrksDapper.Models 2 | { 3 | public class AdvWorksConfig 4 | { 5 | public string DbConnectionString { get; set; } 6 | } 7 | } -------------------------------------------------------------------------------- /AdvWrksDapper/Models/Department.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace AdvWrksDapper.Models 4 | { 5 | /// 6 | /// HR.Department Table of Adventure Works Database 7 | /// 8 | public class Department 9 | { 10 | [Key] 11 | public int DepartmentID { get; set; } 12 | 13 | [Required] 14 | [StringLength(50)] 15 | public string Name { get; set; } 16 | 17 | [Required] 18 | [StringLength(50)] 19 | public string GroupName { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /AdvWrksDapper/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace AdvWrksDapper 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .Build(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /AdvWrksDapper/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:59504/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AdvWrksDapper": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:59505/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /AdvWrksDapper/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 9 - Integration With Database 12 | 13 | ### AdvWrksDapper Project 14 | 15 | Demo code for below feature 16 | 17 | * Connecting, CRUD operation using Dapper with Adventure Works Database 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /AdvWrksDapper/Repository/IDepartmentRepository.cs: -------------------------------------------------------------------------------- 1 | using AdvWrksDapper.Models; 2 | using System.Collections.Generic; 3 | 4 | namespace AdvWrksDapper.Repository 5 | { 6 | public interface IDepartmentRepository 7 | { 8 | IEnumerable GetDepartments(); 9 | Department GetDepartment(int id); 10 | bool DepartmentExists(int id); 11 | bool AddDepartment(Department deptdetails); 12 | bool UpdateDepartment(Department deptdetails); 13 | bool DeleteDepartment(int deptid); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /AdvWrksDapper/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /AdvWrksDapper/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "ApiConfig": { 16 | "DbConnectionString": "Server=.\\sqlexpress;initial catalog=AdventureWorks2014;Trusted_Connection=True;MultipleActiveResultSets=true" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /AsyncWalletDemo/AsyncWalletDemo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-AsyncWalletDemo-E2BCF706-69B1-4A12-BFDE-AA34ED5D8193 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /AsyncWalletDemo/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace AsyncWalletDemo.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /AsyncWalletDemo/ModelsContext/WalletContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace AsyncWalletDemo.ModelsContext 4 | { 5 | public class WalletContext : DbContext 6 | { 7 | public WalletContext(DbContextOptions options) 8 | : base(options) 9 | { 10 | 11 | } 12 | public DbSet Wallet { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /AsyncWalletDemo/ModelsContext/Wallets.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AsyncWalletDemo.ModelsContext 4 | { 5 | //public class Wallets 6 | //{ 7 | // public int Id { get; set; } 8 | // public string Name { get; set; } 9 | //} 10 | 11 | public class DailyExpense 12 | { 13 | public int Id { get; set; } 14 | public string Description { get; set; } 15 | public int Amount { get; set; } 16 | public DateTime SpentDate { get; set; } 17 | public ExpenseType SpentOn { get; set; } 18 | } 19 | 20 | public enum ExpenseType 21 | { 22 | Food, 23 | Movies, 24 | Petrol, 25 | Clothing, 26 | Others 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /AsyncWalletDemo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace AsyncWalletDemo 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /AsyncWalletDemo/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:54041/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AsyncWalletDemo": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:54042/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /AsyncWalletDemo/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 11 - Optimization & Performance 12 | 13 | ### AsyncWalletDemo Project 14 | 15 | Demo code for below feature 16 | 17 | * Asynchronous Web API using async await against database 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /AsyncWalletDemo/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /AsyncWalletDemo/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "ConnectionStrings": { 16 | "SqlDbConnStr": "Server=.\\sqlexpress;initial catalog=WalletDB;Trusted_Connection=True;" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BasicRoutes/BasicRoutes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /BasicRoutes/DomainConstraint.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Http; 2 | using Microsoft.AspNetCore.Routing; 3 | 4 | namespace BasicRoutes 5 | { 6 | public class DomainConstraint : IRouteConstraint 7 | { 8 | public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) 9 | { 10 | var isMatch = false; 11 | if (values["domName"].ToString().Contains("@packt.com")) 12 | { 13 | isMatch = true; 14 | } 15 | return isMatch; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BasicRoutes/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace BasicRoutes 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .Build(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /BasicRoutes/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:56742/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "BasicRoutes": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "environmentVariables": { 22 | "ASPNETCORE_ENVIRONMENT": "Development" 23 | }, 24 | "applicationUrl": "http://localhost:56743/" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /BasicRoutes/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 5 - Implementing Routing 11 | 12 | ### BasicRoutes Project 13 | 14 | Demo code for below feature 15 | 16 | * Routing basics 17 | * Route Builder 18 | * Route Constraints 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/BudegetIdentityDemo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-BudegetIdentityDemo-560A7AD1-1116-4B5A-93AD-ECD8B7F6CF54 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/Controllers/AuthController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Identity; 2 | using Microsoft.AspNetCore.Identity.EntityFrameworkCore; 3 | using Microsoft.AspNetCore.Mvc; 4 | using System.Threading.Tasks; 5 | 6 | namespace BudegetIdentityDemo.Controllers 7 | { 8 | public class AuthController : Controller 9 | { 10 | private readonly SignInManager _signInMgr; 11 | 12 | public AuthController(SignInManager signInMgr) 13 | { 14 | _signInMgr = signInMgr; 15 | } 16 | 17 | [HttpPost("api/auth/login")] 18 | public async Task Login([FromBody] CredentialModel model) 19 | { 20 | var result = await _signInMgr.PasswordSignInAsync(model.UserName, model.Password, false, false); 21 | if (result.Succeeded) 22 | { 23 | return Ok(); 24 | } 25 | return BadRequest("Failed to login"); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /BudegetIdentityDemo/Controllers/BudgetCategoryController.cs: -------------------------------------------------------------------------------- 1 | using BudegetIdentityDemo.Models; 2 | using Microsoft.AspNetCore.Authorization; 3 | using Microsoft.AspNetCore.Mvc; 4 | using System.Collections.Generic; 5 | 6 | 7 | namespace BudegetIdentityDemo.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | [Authorize] 11 | public class BudgetCategoryController : Controller 12 | { 13 | // GET: api/values 14 | [HttpGet] 15 | public IActionResult Get() 16 | { 17 | return Ok(GetItems()); 18 | } 19 | 20 | private IList GetItems() 21 | { 22 | var listitems = new List 23 | { 24 | new BudgetCategory() { Id = 10, Name = "Food", Amount = 200 } 25 | }; 26 | return listitems; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/Controllers/CredentialModel.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace BudegetIdentityDemo.Controllers 4 | { 5 | public class CredentialModel 6 | { 7 | [Required] 8 | public string UserName { get; set; } 9 | [Required] 10 | public string Password { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /BudegetIdentityDemo/Controllers/VerifyCodeViewModel.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace BudegetIdentityDemo.Controllers 4 | { 5 | public class VerifyCodeViewModel 6 | { 7 | [Required] 8 | public string Provider { get; set; } 9 | 10 | [Required] 11 | public string Code { get; set; } 12 | 13 | public string ReturnUrl { get; set; } 14 | 15 | [Display(Name = "Remember this browser?")] 16 | public bool RememberBrowser { get; set; } 17 | 18 | [Display(Name = "Remember me?")] 19 | public bool RememberMe { get; set; } 20 | } 21 | } -------------------------------------------------------------------------------- /BudegetIdentityDemo/Models/BudgetCategory.cs: -------------------------------------------------------------------------------- 1 | namespace BudegetIdentityDemo.Models 2 | { 3 | public class BudgetCategory 4 | { 5 | public int Id { get; set; } 6 | public string Name { get; set; } 7 | public int Amount { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace BudegetIdentityDemo 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .UseDefaultServiceProvider(options => 24 | options.ValidateScopes = false) 25 | .Build(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:58016/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "BudegetIdentityDemo": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:58017/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server Express Edition 10 | 11 | ## Chapter 8 - Web API Security 12 | 13 | ### BudegetIdentityDemo Project 14 | 15 | Demo code for below feature 16 | 17 | * Use Identity Package 18 | * Cookie Authentication 19 | * Two Factor Authentication 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /BudegetIdentityDemo/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "ConnectionStrings": { 16 | "BudgetConnStr": "Data Source=.\\SQLEXPRESS;Initial Catalog=BudgetDbDemo;Integrated Security=True;Connect Timeout=30;" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CustomRouteConstraints/Controllers/EmployeeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace CustomRouteConstraints.Controllers 4 | { 5 | //Custom route constraint in Attribute routes 6 | public class EmployeeController : Controller 7 | { 8 | [HttpGet("api/employee/{domName:domain}")] 9 | public string Get(string domName) 10 | { 11 | return ($"Domain Name Constraint Passed - {domName}"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CustomRouteConstraints/CustomRouteConstraints.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-CustomRouteConstraints-64EA6F66-EBA5-4A8D-A9E4-7A406C9EE3FA 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /CustomRouteConstraints/DomainConstraint.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Http; 2 | using Microsoft.AspNetCore.Routing; 3 | 4 | namespace CustomRouteConstraints 5 | { 6 | public class DomainConstraint : IRouteConstraint 7 | { 8 | public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) 9 | { 10 | var isMatch = false; 11 | if (values["domName"].ToString().Contains("@packt.com")) 12 | { 13 | isMatch = true; 14 | } 15 | return isMatch; 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /CustomRouteConstraints/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace CustomRouteConstraints 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /CustomRouteConstraints/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:53947/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "CustomRouteConstraints": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:53948/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /CustomRouteConstraints/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 5 - Implementing Routing 11 | 12 | ### CustomRouteConstraints Project 13 | 14 | Demo code for below feature 15 | 16 | 17 | * Writing Custom Route Constraints 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /CustomRouteConstraints/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /CustomRouteConstraints/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LinkGeneration/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace LinkGeneration.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LinkGeneration/LinkGeneration.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-LinkGeneration-EB59C772-A4A9-4488-90FB-AC5EAC74994E 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /LinkGeneration/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace LinkGeneration 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LinkGeneration/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:54115/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "LinkGeneration": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:54116/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LinkGeneration/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 5 - Implementing Routing 11 | 12 | ### LinkGeneration Project 13 | 14 | Demo code for below feature 15 | 16 | 17 | * Link generation in Routes 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /LinkGeneration/Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | using Microsoft.Extensions.Options; 11 | 12 | namespace LinkGeneration 13 | { 14 | public class Startup 15 | { 16 | // This method gets called by the runtime. Use this method to add services to the container. 17 | public void ConfigureServices(IServiceCollection services) 18 | { 19 | services.AddMvc(); 20 | } 21 | 22 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 23 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 24 | { 25 | app.UseMvc(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LinkGeneration/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LinkGeneration/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace MyFirstCoreAPI.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/MyFirstCoreAPI.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-MyFirstCoreAPI-D0B356AB-BC35-4D73-9576-997BC358BEE9 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace MyFirstCoreAPI 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .Build(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:49396/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "MyFirstCoreAPI": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:49397/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | 9 | 10 | ## Chapter 3 - Anatomy of ASP.NET Core Web API 11 | 12 | ### MyFirstCoreAPI 13 | 14 | Default Web API project created by ASP.NET Core 2.0 15 | 16 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace MyFirstCoreAPI 6 | { 7 | public class Startup 8 | { 9 | // This method gets called by the runtime. Use this method to add services to the container. 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | services.AddMvc(); 13 | } 14 | 15 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 16 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 17 | { 18 | app.UseMvc(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MyFirstCoreAPI/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/Controllers/PacktsController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace MyFirstCoreApiRoutes.Controllers 4 | { 5 | // Example of Multiple Routes 6 | [Route("Stocks")] 7 | [Route("[controller]")] 8 | public class PacktsController : Controller 9 | { 10 | [HttpGet("Check")] 11 | [HttpGet("Available")] 12 | public string GetStock() 13 | { 14 | return "Its multiple route"; 15 | } 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/MyFirstCoreApiRoutes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-MyFirstCoreApiRoutes-01662C58-3432-4789-B8BB-25DD825A56D2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace MyFirstCoreApiRoutes 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:53794/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "MyFirstCoreApiRoutes": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:53795/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 5 - Implementing Routing 11 | 12 | ### MyFirstCoreAPIRoutes Project 13 | 14 | Demo code for below feature 15 | 16 | * Attribute Routing 17 | * Route Constraints 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace MyFirstCoreApiRoutes 6 | { 7 | public class Startup 8 | { 9 | // This method gets called by the runtime. Use this method to add services to the container. 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | services.AddMvc(); 13 | } 14 | 15 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 16 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 17 | { 18 | app.UseMvc(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MyFirstCoreApiRoutes/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MyWallet/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace MyWallet.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MyWallet/ErrorHandlers/WebAPIError.cs: -------------------------------------------------------------------------------- 1 | namespace MyWallet.ErrorHandlers 2 | { 3 | public class WebAPIError 4 | { 5 | public string message { get; set; } 6 | public bool isError { get; set; } 7 | public string detail { get; set; } 8 | 9 | public WebAPIError(string message) 10 | { 11 | this.message = message; 12 | isError = true; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /MyWallet/ErrorHandlers/WebApiException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | 4 | namespace MyWallet.ErrorHandlers 5 | { 6 | public class WebApiException : Exception 7 | { 8 | public HttpStatusCode StatusCode { get; set; } 9 | 10 | public WebApiException(string message, 11 | HttpStatusCode statusCode = HttpStatusCode.InternalServerError) : 12 | base(message) 13 | { 14 | StatusCode = statusCode; 15 | } 16 | public WebApiException(Exception ex, HttpStatusCode statusCode = HttpStatusCode.InternalServerError) : base(ex.Message) 17 | { 18 | StatusCode = statusCode; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyWallet/ModelsContexts/DailyExpense.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | 4 | namespace MyWallet.ModelsContexts 5 | { 6 | 7 | public class DailyExpense 8 | { 9 | [Key] 10 | public int Id { get; set; } 11 | public string Description { get; set; } 12 | public int Amount { get; set; } 13 | public DateTime SpentDate { get; set; } 14 | public ExpenseType SpentOn { get; set; } 15 | } 16 | 17 | public enum ExpenseType 18 | { 19 | Food, 20 | Movies, 21 | Petrol, 22 | Clothing, 23 | Others 24 | } 25 | } -------------------------------------------------------------------------------- /MyWallet/ModelsContexts/ExpenseContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace MyWallet.ModelsContexts 4 | { 5 | public class ExpenseContext : DbContext 6 | { 7 | public ExpenseContext(DbContextOptions options) 8 | :base(options) 9 | { 10 | 11 | } 12 | public DbSet DailyExpenses { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MyWallet/MyWallet.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-MyWallet-7BB25584-FE81-483A-9F87-E2C66B5B045E 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /MyWallet/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace MyWallet 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyWallet/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:64917/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "MyWallet": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:64918/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MyWallet/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 10 - Error Handling, Tracing & Logging 12 | 13 | ### MyWallet Project 14 | 15 | Demo code for below feature 16 | 17 | * Logging to file using NLog 18 | * Database logging using Serilog 19 | * Exception Management 20 | 21 | 22 | 23 | Use **Postman** Rest client to test 24 | -------------------------------------------------------------------------------- /MyWallet/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MyWallet/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "Serilog": { 16 | "ConnectionString": "Server=.\\sqlexpress;Database=MyWalletLogDB;trusted_connection=true", 17 | "TableName": "Logs" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MyWallet/nlog.config: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /PacktContacts/.dockerignore: -------------------------------------------------------------------------------- 1 | * 2 | !obj/Docker/publish/* 3 | !obj/Docker/empty/ 4 | -------------------------------------------------------------------------------- /PacktContacts/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace PacktContacts.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PacktContacts/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM microsoft/aspnetcore:2.0 2 | ARG source 3 | WORKDIR /app 4 | EXPOSE 80 5 | COPY ${source:-obj/Docker/publish} . 6 | ENTRYPOINT ["dotnet", "PacktContacts.dll"] 7 | -------------------------------------------------------------------------------- /PacktContacts/Model/AppUser.cs: -------------------------------------------------------------------------------- 1 | namespace PacktContacts.Models 2 | { 3 | public class AppUser 4 | { 5 | public int Id { get; set; } 6 | public string UserName { get; set; } 7 | public string Password { get; set; } 8 | public bool IsSuperUser { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PacktContacts/Model/Contacts.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace PacktContacts.Model 4 | { 5 | public class Contacts 6 | { 7 | public int Id { get; set; } 8 | public string FirstName { get; set; } 9 | public string LastName { get; set; } 10 | public string Email { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /PacktContacts/Model/CredentialsModel.cs: -------------------------------------------------------------------------------- 1 | namespace PacktContacts.Models 2 | { 3 | public class CredentialsModel 4 | { 5 | public string Username { get; set; } 6 | public string Password { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PacktContacts/Model/PacktContactsContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using PacktContacts.Model; 3 | 4 | namespace PacktContacts.Models 5 | { 6 | public class PacktContactsContext : DbContext 7 | { 8 | public PacktContactsContext(DbContextOptions options) 9 | :base(options) 10 | { 11 | } 12 | public DbSet Contacts { get; set; } 13 | public DbSet AppUsers { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /PacktContacts/Model/TestData.cs: -------------------------------------------------------------------------------- 1 | namespace PacktContacts.Models 2 | { 3 | public class TestData 4 | { 5 | public static void AddTestData(PacktContactsContext context) 6 | { 7 | var testUser1 = new AppUser 8 | { 9 | Id = 1, 10 | UserName = "mithunvp", 11 | Password = "abcd123", 12 | IsSuperUser = true 13 | }; 14 | 15 | var testUser2 = new AppUser 16 | { 17 | Id = 2, 18 | UserName = "Yogi", 19 | Password = "abcd123", 20 | IsSuperUser = false 21 | }; 22 | 23 | //context.AppUsers.AddRange(testUser1, testUser2); 24 | 25 | context.SaveChanges(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PacktContacts/PacktContacts.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | win7-x64 6 | aspnet-PacktContacts-00387BAF-E1DB-4036-B961-C9AF69EB6031 7 | ..\docker-compose.dcproj 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /PacktContacts/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace PacktContacts 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .UseDefaultServiceProvider(options => 17 | options.ValidateScopes = false) 18 | .Build(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /PacktContacts/Properties/PublishProfiles/FolderProfile.pubxml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | FileSystem 9 | FileSystem 10 | Release 11 | Any CPU 12 | 13 | True 14 | False 15 | d7a1ba59-e090-4bff-af58-9f21ef107f0f 16 | D:\publishOutput 17 | False 18 | 19 | -------------------------------------------------------------------------------- /PacktContacts/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:50460/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "PacktContacts": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:50461/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /PacktContacts/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 12 - Hosting & Deployment 12 | 13 | ### PacktContacts Project 14 | 15 | This project used for deploying and hosting on various environments. 16 | 17 | Also this chapter used as demo Web API for **Chapter 13 - Modern Web Frontends** 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /PacktContacts/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PacktContacts/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "Tokens": { 16 | "Key": "Mastering@spnetC0re@P!", 17 | "Issuer": "http://packtdemo.io", 18 | "Audience": "http://packtdemo.io" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /PacktContactsCore/Context/ContactsContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using PacktContactsCore.Model; 3 | 4 | namespace PacktContactsCore.Context 5 | { 6 | public class ContactsContext : DbContext 7 | { 8 | public ContactsContext(DbContextOptions options) 9 | : base(options) { } 10 | public ContactsContext() { 11 | } 12 | public DbSet Contacts { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PacktContactsCore/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace PacktContactsCore.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PacktContactsCore/Model/Contacts.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | 4 | namespace PacktContactsCore.Model 5 | { 6 | public class Contacts 7 | { 8 | [Key] 9 | public int Id { get; set; } 10 | [Required] 11 | [MinLength(4)] 12 | public string FirstName { get; set; } 13 | public string LastName { get; set; } 14 | [Required] 15 | public string Email { get; set; } 16 | public DateTime DateOfBirth { get; set; } 17 | } 18 | } -------------------------------------------------------------------------------- /PacktContactsCore/PacktContactsCore.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-PacktContactsCore-932E81D6-CE6C-43EE-8D77-C6C53DEE4DDE 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /PacktContactsCore/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace PacktContactsCore 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /PacktContactsCore/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:55044/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "PacktContactsCore": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:55045/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /PacktContactsCore/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 9 - Integration With Database 12 | 13 | ### PacktContactsCore 14 | 15 | Demo code for below feature 16 | 17 | * Connecting, CRUD operation with custom database 18 | * Using Entity Framework Core 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /PacktContactsCore/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PacktContactsCore/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "ConnectionStrings": { 16 | "SqlDbConnStr": "Server=.\\sqlexpress;initial catalog=PacktContactsDB;Trusted_Connection=True;" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /PacktContactsJsUI/App_Start/ReactConfig.cs: -------------------------------------------------------------------------------- 1 | using React; 2 | 3 | [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(PacktContactsJsUI.ReactConfig), "Configure")] 4 | 5 | namespace PacktContactsJsUI 6 | { 7 | public static class ReactConfig 8 | { 9 | public static void Configure() 10 | { 11 | // If you want to use server-side rendering of React components, 12 | // add all the necessary JavaScript files here. This includes 13 | // your components as well as all of their dependencies. 14 | // See http://reactjs.net/ for more information. Example: 15 | //ReactSiteConfiguration.Configuration 16 | // .AddScript("~/Scripts/First.jsx") 17 | // .AddScript("~/Scripts/Second.jsx"); 18 | 19 | // If you use an external build too (for example, Babel, Webpack, 20 | // Browserify or Gulp), you can improve performance by disabling 21 | // ReactJS.NET's version of Babel and loading the pre-transpiled 22 | // scripts. Example: 23 | //ReactSiteConfiguration.Configuration 24 | // .SetLoadBabel(false) 25 | // .AddScriptWithoutTransform("~/Scripts/bundle.server.js") 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /PacktContactsJsUI/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET web application or MVC 5 6 | * Visual Studio IDE 7 | * Your favourite browser 8 | * Sql Server 2014 Express Edition 9 | 10 | ## Chapter 13 - Modern Web Frontends 11 | 12 | ### PacktContactsJsUI Project 13 | 14 | Demo for consuming *PacktContacts* Web API created in Chapter 12 15 | 16 | * Consuming using JavaScript 17 | * Consuming using JQuery 18 | * Consuming using ReactJS 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /PacktContactsJsUI/ReactUI.html: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Consuming Web API using ReactJS 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 |

Consuming ASP.NET Core Web API using ReactJS

18 |
19 |
20 |
21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /PacktContactsJsUI/Scripts/react/react-dom-server.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ReactDOMServer v0.14.7 3 | * 4 | * Copyright 2013-2015, Facebook, Inc. 5 | * All rights reserved. 6 | * 7 | * This source code is licensed under the BSD-style license found in the 8 | * LICENSE file in the root directory of this source tree. An additional grant 9 | * of patent rights can be found in the PATENTS file in the same directory. 10 | * 11 | */ 12 | !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e(require("react"));else if("function"==typeof define&&define.amd)define(["react"],e);else{var f;f="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,f.ReactDOMServer=e(f.React)}}(function(e){return e.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED}); -------------------------------------------------------------------------------- /PacktContactsJsUI/Scripts/react/react-dom.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ReactDOM v0.14.7 3 | * 4 | * Copyright 2013-2015, Facebook, Inc. 5 | * All rights reserved. 6 | * 7 | * This source code is licensed under the BSD-style license found in the 8 | * LICENSE file in the root directory of this source tree. An additional grant 9 | * of patent rights can be found in the PATENTS file in the same directory. 10 | * 11 | */ 12 | !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e(require("react"));else if("function"==typeof define&&define.amd)define(["react"],e);else{var f;f="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,f.ReactDOM=e(f.React)}}(function(e){return e.__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED}); -------------------------------------------------------------------------------- /PacktContactsJsUI/Views/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PacktContactsJsUI/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/PacktContactsJsUI/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /PacktContactsJsUI/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/PacktContactsJsUI/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /PacktContactsJsUI/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/PacktContactsJsUI/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /PacktContactsJsUI/fonts/glyphicons-halflings-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/PacktContactsJsUI/fonts/glyphicons-halflings-regular.woff2 -------------------------------------------------------------------------------- /PersonalBudget/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace PersonalBudget.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PersonalBudget/IdProtectorConverter.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Microsoft.AspNetCore.DataProtection; 3 | using PersonalBudget.Models; 4 | 5 | namespace PersonalBudget 6 | { 7 | public class IdProtectorConverter : ITypeConverter 8 | { 9 | private readonly IDataProtector protector; 10 | 11 | public IdProtectorConverter(IDataProtectionProvider protectionprovider, StringConstants strconsts) 12 | { 13 | this.protector = protectionprovider.CreateProtector(strconsts.IdQryStr); 14 | } 15 | public BudgetCategoryDTO Convert(BudgetCategory source, BudgetCategoryDTO destination, ResolutionContext context) 16 | { 17 | return new BudgetCategoryDTO 18 | { 19 | Name = source.Name, 20 | Amount = source.Amount, 21 | EncryptId = this.protector.Protect(source.Id.ToString()) 22 | }; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /PersonalBudget/Models/AppUser.cs: -------------------------------------------------------------------------------- 1 | namespace PersonalBudget.Models 2 | { 3 | public class AppUser 4 | { 5 | public int Id { get; set; } 6 | public string UserName { get; set; } 7 | public string Password { get; set; } 8 | public bool IsSuperUser { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PersonalBudget/Models/BudgetCategory.cs: -------------------------------------------------------------------------------- 1 | namespace PersonalBudget.Models 2 | { 3 | public class BudgetCategory 4 | { 5 | public int Id { get; set; } 6 | public string Name { get; set; } 7 | public int Amount { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /PersonalBudget/Models/BudgetCategoryDTO.cs: -------------------------------------------------------------------------------- 1 | namespace PersonalBudget.Models 2 | { 3 | public class BudgetCategoryDTO 4 | { 5 | public string EncryptId { get; set; } 6 | public string Name { get; set; } 7 | public int Amount { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /PersonalBudget/Models/CredentialsModel.cs: -------------------------------------------------------------------------------- 1 | namespace PersonalBudget.Models 2 | { 3 | public class CredentialsModel 4 | { 5 | public string Username { get; set; } 6 | public string Password { get; set; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PersonalBudget/Models/PersonalBudgetContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace PersonalBudget.Models 4 | { 5 | public class PersonalBudgetContext : DbContext 6 | { 7 | public PersonalBudgetContext(DbContextOptions options) 8 | :base(options) 9 | { 10 | 11 | } 12 | public DbSet BudgetCategories { get; set; } 13 | public DbSet AppUsers { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /PersonalBudget/Models/TestData.cs: -------------------------------------------------------------------------------- 1 | namespace PersonalBudget.Models 2 | { 3 | public class TestData 4 | { 5 | public static void AddTestData(PersonalBudgetContext context) 6 | { 7 | var testUser1 = new AppUser 8 | { 9 | Id = 1, 10 | UserName = "mithunvp", 11 | Password = "abcd123", 12 | IsSuperUser = true 13 | }; 14 | 15 | var testUser2 = new AppUser 16 | { 17 | Id = 2, 18 | UserName = "Yogi", 19 | Password = "abcd123", 20 | IsSuperUser = false 21 | }; 22 | 23 | context.AppUsers.AddRange(testUser1, testUser2); 24 | 25 | context.SaveChanges(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PersonalBudget/PersonalBudget.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-PersonalBudget-C4AE4DD8-0E4F-4A38-9A90-546AFBC73481 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /PersonalBudget/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace PersonalBudget 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .UseDefaultServiceProvider(options => 24 | options.ValidateScopes = false) 25 | .Build(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /PersonalBudget/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:51052/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "PersonalBudget": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:51053/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /PersonalBudget/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 8 - Web API Security 11 | 12 | ### PersonalBudget Project 13 | 14 | Demo code for below feature 15 | 16 | * Applying CORS 17 | * Data Protection API 18 | * JWT 19 | * Claims based Authorizations 20 | 21 | 22 | Use **Postman** Rest client to test 23 | -------------------------------------------------------------------------------- /PersonalBudget/StringConstants.cs: -------------------------------------------------------------------------------- 1 | namespace PersonalBudget 2 | { 3 | public class StringConstants 4 | { 5 | public string IdQryStr => "AppIdString"; //Name can be anything. 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PersonalBudget/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PersonalBudget/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | }, 15 | "Tokens": { 16 | "Key": "Mastering@spnetC0re@P!", 17 | "Issuer": "http://packtdemo.io", 18 | "Audience": "http://packtdemo.io" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Business/Interfaces/IStoreManager.cs: -------------------------------------------------------------------------------- 1 | using Puhoi.Models; 2 | using Puhoi.Models.Models; 3 | using System; 4 | 5 | namespace Puhoi.Business.Interfaces 6 | { 7 | public interface IStoreManager 8 | { 9 | HttpModelResult Add(BaseModel model); 10 | HttpModelResult Update(BaseModel model, Guid id); 11 | HttpModelResult Get(Guid id); 12 | HttpModelResult Delete(Guid id); 13 | HttpModelResult GetAll(); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Business/Managers/ProductManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Puhoi.Business.Managers 8 | { 9 | public class ProductManager 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Business/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Common/FluentValidationExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | 6 | namespace Puhoi.Common 7 | { 8 | public static class FluentValidationExtension 9 | { 10 | public static IEnumerable ToValidationResult( 11 | this FluentValidation.Results.ValidationResult validationResult) 12 | { 13 | IEnumerable results = 14 | validationResult.Errors.Select( 15 | item => new ValidationResult(item.ErrorMessage, new List { item.PropertyName })); 16 | return results; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Common/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Data/IDataStore.cs: -------------------------------------------------------------------------------- 1 | using Puhoi.DataModels; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace Puhoi.Data 6 | { 7 | public interface IDataStore 8 | { 9 | BaseDto AddorUpdate(BaseDto dto); 10 | BaseDto Delete(Guid id); 11 | BaseDto Get(Guid id); 12 | BaseDto Get(string name); 13 | IEnumerable GetAll(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.DataModels/BaseDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puhoi.DataModels 4 | { 5 | public class BaseDto 6 | { 7 | public Guid UId { get; set; } 8 | public string Name { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.DataModels/StoreDto.cs: -------------------------------------------------------------------------------- 1 | namespace Puhoi.DataModels 2 | { 3 | public class StoreDto : BaseDto 4 | { 5 | public long StoreId { get; set; } 6 | public int NumberOfProducts { get; set; } 7 | public string DisplayName { get; set; } 8 | public string Description { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models.Tests/GreenProductModel.cs: -------------------------------------------------------------------------------- 1 | using Puhoi.Models.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Puhoi.Models.Tests 9 | { 10 | public static class GreenProductModel 11 | { 12 | public static ProductModel Model() 13 | { 14 | return new ProductModel 15 | { 16 | Id = Guid.NewGuid(), 17 | Name = "Gold Strong Blue Cheese", 18 | Description = "Award winning Blue Cheese from Puhoi Valley", 19 | Count = 10000, 20 | Size = "Small" 21 | }; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models.Tests/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/HttpModelResult.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Puhoi.Models.Models; 3 | using System.Net; 4 | 5 | namespace Puhoi.Models 6 | { 7 | public class HttpModelResult 8 | { 9 | public HttpStatusCode HttpStatus { get; set; } 10 | public BaseModel Model { get; set; } 11 | public IEnumerable Models {get;set;} 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/Models/BaseModel.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | using Puhoi.Common; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.ComponentModel.DataAnnotations; 6 | 7 | namespace Puhoi.Models.Models 8 | { 9 | public class BaseModel 10 | { 11 | public Guid Id { get; set; } 12 | public string Name { get; set; } 13 | 14 | //public IEnumerable Validate(System.ComponentModel.DataAnnotations.ValidationContext validationContext) 15 | //{ 16 | // return GetValidator().Validate(this).ToValidationResult(); 17 | //} 18 | 19 | //protected abstract IValidator GetValidator(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/Models/ProductModel.cs: -------------------------------------------------------------------------------- 1 | using Puhoi.Models.Validators; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace Puhoi.Models.Models 7 | { 8 | public class ProductModel : BaseModel, IValidatableObject 9 | { 10 | public string Description { get; set;} 11 | public string Name { get; set; } 12 | public int Count { get; set; } 13 | public string Size { get; set; } 14 | 15 | public IEnumerable Validate(ValidationContext validationContext) 16 | { 17 | ProductModelValidator validator = new ProductModelValidator(); 18 | FluentValidation.Results.ValidationResult result = validator.Validate(this); 19 | return result.Errors.Select(item => 20 | new ValidationResult(item.ErrorMessage, new[] { item.PropertyName })); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/Models/StoreModel.cs: -------------------------------------------------------------------------------- 1 |  2 | using Puhoi.Models.Validators; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace Puhoi.Models.Models 8 | { 9 | public class StoreModel : BaseModel, IValidatableObject 10 | { 11 | public int NumberOfProducts { get; set; } 12 | public string DisplayName { get; set; } 13 | public string Description { get; set; } 14 | 15 | public IEnumerable Validate(ValidationContext validationContext) 16 | { 17 | StoreModelValidator validator = new StoreModelValidator(); 18 | FluentValidation.Results.ValidationResult result = validator.Validate(this); 19 | return result.Errors.Select(item => 20 | new ValidationResult(item.ErrorMessage, new[] { item.PropertyName })); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/Validators/BaseValidator.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using FluentValidation; 4 | 5 | namespace Puhoi.Models.Validators 6 | { 7 | public class BaseValidator : AbstractValidator 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/Validators/ProductModelValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | using Puhoi.Models.Models; 3 | 4 | namespace Puhoi.Models.Validators 5 | { 6 | public class ProductModelValidator : BaseValidator 7 | { 8 | public ProductModelValidator() 9 | { 10 | RuleFor(p => p.Name).NotEmpty(); 11 | RuleFor(p => p.Description).NotEmpty(); 12 | RuleFor(p => p.Size).NotEmpty(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/Validators/StoreModelValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | using Puhoi.Models.Models; 3 | 4 | namespace Puhoi.Models.Validators 5 | { 6 | public class StoreModelValidator : BaseValidator 7 | { 8 | public StoreModelValidator() 9 | { 10 | RuleFor(s => s.Name).NotNull().Length(1,100); 11 | RuleFor(s => s.Description).NotNull().Length(1, 100); 12 | RuleFor(s => s.DisplayName).NotNull().Length(1, 100); 13 | RuleFor(s => s.NumberOfProducts).NotNull().InclusiveBetween(1,50); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/Puhoi.Models/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/PuhoiAPI.Tests/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Common.Core/FluentValidationExtension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Linq; 5 | 6 | namespace Puhoi.Common.Core 7 | { 8 | public static class FluentValidationExtension 9 | { 10 | public static IEnumerable ToValidationResult( 11 | this FluentValidation.Results.ValidationResult validationResult) 12 | { 13 | IEnumerable results = 14 | validationResult.Errors.Select( 15 | item => new ValidationResult(item.ErrorMessage, new List { item.PropertyName })); 16 | return results; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Common.Core/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("Puhoi.Common.Core")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("f2d3f457-203c-404d-a481-4e51b9902ae1")] 20 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Common.Core/Puhoi.Common.Core.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0 5 | Puhoi.Common.Core 6 | Puhoi.Common.Core 7 | false 8 | false 9 | false 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/HttpModelResult.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Puhoi.Models.Models; 3 | using System.Net; 4 | using Puhoi.Models.Core.Models; 5 | 6 | namespace Puhoi.Models 7 | { 8 | public class HttpModelResult 9 | { 10 | public HttpStatusCode HttpStatus { get; set; } 11 | public BaseModel Model { get; set; } 12 | public IEnumerable Models {get;set;} 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Models/BaseModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Puhoi.Models.Core.Models 4 | { 5 | public class BaseModel 6 | { 7 | public Guid Id { get; set; } 8 | public string Name { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Models/ProductModel.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.Linq; 4 | using Puhoi.Models.Core.Validators; 5 | 6 | namespace Puhoi.Models.Core.Models 7 | { 8 | public class ProductModel : BaseModel, IValidatableObject 9 | { 10 | public string Description { get; set;} 11 | public string Name { get; set; } 12 | public int Count { get; set; } 13 | public string Size { get; set; } 14 | 15 | public IEnumerable Validate(ValidationContext validationContext) 16 | { 17 | ProductModelValidator validator = new ProductModelValidator(); 18 | FluentValidation.Results.ValidationResult result = validator.Validate(this); 19 | return result.Errors.Select(item => 20 | new ValidationResult(item.ErrorMessage, new[] { item.PropertyName })); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Models/StoreModel.cs: -------------------------------------------------------------------------------- 1 |  2 | using Puhoi.Models.Validators; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using Puhoi.Models.Core.Models; 7 | using Puhoi.Models.Core.Validators; 8 | 9 | namespace Puhoi.Models.Models 10 | { 11 | public class StoreModel : BaseModel, IValidatableObject 12 | { 13 | //public string Name { get; set; } 14 | public int NumberOfProducts { get; set; } 15 | public string DisplayName { get; set; } 16 | public string Description { get; set; } 17 | 18 | public IEnumerable Validate(ValidationContext validationContext) 19 | { 20 | StoreModelValidator validator = new StoreModelValidator(); 21 | FluentValidation.Results.ValidationResult result = validator.Validate(this); 22 | return result.Errors.Select(item => 23 | new ValidationResult(item.ErrorMessage, new[] { item.PropertyName })); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("Puhoi.Models.Core")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("42047007-b053-42ed-a862-959ca5c75485")] 20 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Puhoi.Models.Core.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0 5 | Puhoi.Models.Core 6 | Puhoi.Models.Core 7 | false 8 | false 9 | false 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Validators/BaseValidator.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using FluentValidation; 4 | 5 | namespace Puhoi.Models.Validators 6 | { 7 | public class BaseValidator : AbstractValidator 8 | { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Validators/ProductModelValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | using Puhoi.Models.Core.Models; 3 | using Puhoi.Models.Models; 4 | using Puhoi.Models.Validators; 5 | 6 | namespace Puhoi.Models.Core.Validators 7 | { 8 | public class ProductModelValidator : BaseValidator 9 | { 10 | public ProductModelValidator() 11 | { 12 | RuleFor(p => p.Name).NotEmpty(); 13 | RuleFor(p => p.Description).NotEmpty(); 14 | RuleFor(p => p.Size).NotEmpty(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.Core/Validators/StoreModelValidator.cs: -------------------------------------------------------------------------------- 1 | using FluentValidation; 2 | using Puhoi.Models.Models; 3 | using Puhoi.Models.Validators; 4 | 5 | namespace Puhoi.Models.Core.Validators 6 | { 7 | public class StoreModelValidator : BaseValidator 8 | { 9 | public StoreModelValidator() 10 | { 11 | RuleFor(s => s.Name).NotNull().Length(1,100); 12 | RuleFor(s => s.Description).NotNull().Length(1, 100); 13 | RuleFor(s => s.DisplayName).NotNull().Length(1, 100); 14 | RuleFor(s => s.NumberOfProducts).NotNull().InclusiveBetween(1,50); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.xUnit.Tests/GreenProductModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Puhoi.Models.Core.Models; 3 | 4 | namespace Puhoi.Models.xUnit.Tests 5 | { 6 | public static class GreenProductModel 7 | { 8 | public static ProductModel Model() 9 | { 10 | return new ProductModel 11 | { 12 | Id = Guid.NewGuid(), 13 | Name = "Gold Strong Blue Cheese", 14 | Description = "Award winning Blue Cheese from Puhoi Valley", 15 | Count = 10000, 16 | Size = "Small" 17 | }; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/Puhoi.Models.xUnit.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("PuhoiAPI.xUnit.Tests")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("1f6d8882-a0a0-49df-a406-627466c4befa")] 20 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI.xUnit.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("PuhoiAPI.xUnit.Tests")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("1a8e0e63-9bfa-4e45-b430-d5d5b612c2d5")] 20 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI.xUnit.Tests/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "baseUri": "http://localhost:5000/api/" 3 | } -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI/Attributes/ModelValidationAttribute.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | using Microsoft.AspNetCore.Mvc; 5 | using Microsoft.AspNetCore.Mvc.Filters; 6 | 7 | namespace PuhoiAPI.Attributes 8 | { 9 | public class ModelValidationAttribute : ActionFilterAttribute 10 | { 11 | public override void OnActionExecuting(ActionExecutingContext context) 12 | { 13 | if( !context.ModelState.IsValid ) 14 | { 15 | context.Result = new BadRequestObjectResult(context.ModelState); 16 | } 17 | base.OnActionExecuting(context); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI/AutoMapperProfileConfiguration.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Puhoi.DataModels; 3 | using Puhoi.Models.Models; 4 | 5 | namespace PuhoiAPI 6 | { 7 | public class AutoMapperProfileConfiguration : Profile 8 | { 9 | public AutoMapperProfileConfiguration() 10 | { 11 | CreateMap(). 12 | ForSourceMember("StoreId", s => s.Ignore()). 13 | ForSourceMember("UId", s => s.Ignore()). 14 | ForMember("Id", s => s.Ignore()). 15 | AfterMap((dto, model) => model.Id = dto.UId); 16 | 17 | CreateMap() 18 | .AfterMap((model, dto) => dto.UId = model.Id); 19 | 20 | } 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.AspNetCore.Builder; 8 | 9 | namespace WebApplication1 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .Build(); 21 | 22 | host.Run(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI/app.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Puhoi/Puhoi-master/src/PuhoiAPI/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Specific instructions to run code files.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/Specific instructions to run code files.pdf -------------------------------------------------------------------------------- /basics-logging/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace basics_logging 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .Build(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /basics-logging/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:63720/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "basics_logging": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:63721/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /basics-logging/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 10 - Error Handling, Tracing & Logging 12 | 13 | ### basics-logging Project 14 | 15 | Demo code for below feature 16 | 17 | * Basics of logging in ASP.NET Core 18 | 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /basics-logging/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /basics-logging/appsettings.json: -------------------------------------------------------------------------------- 1 |  { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "basic_logging_demo": "Warning" 6 | } 7 | } 8 | } 9 | 10 | 11 | -------------------------------------------------------------------------------- /basics-logging/basics-logging.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-basics_logging-AA5CDADD-2897-42A0-82DB-E99EC7216BF2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /builtin-middleware/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace builtin_middleware 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /builtin-middleware/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:64971/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "builtin_middleware": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "environmentVariables": { 22 | "ASPNETCORE_ENVIRONMENT": "Development" 23 | }, 24 | "applicationUrl": "http://localhost:64972/" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /builtin-middleware/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 6 - Middleware & Filters 11 | 12 | ### Builtin-Middleware Project 13 | 14 | Demo code for below feature 15 | 16 | * Use of builtin middleware provided by ASP.NET Core 17 | 18 | 19 | Use **Postman** Rest client to test 20 | -------------------------------------------------------------------------------- /builtin-middleware/builtin-middleware.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /builtin-middleware/wwwroot/index.html: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Mastering Web API 6 | 7 | 8 |

Served by StaticFile middleware.

9 | 10 | -------------------------------------------------------------------------------- /compression-cache-demo/Controllers/PersonController.cs: -------------------------------------------------------------------------------- 1 | using GenFu; 2 | using Microsoft.AspNetCore.Mvc; 3 | using System; 4 | 5 | namespace compression_cache_demo.Controllers 6 | { 7 | [Route("api/[controller]")] 8 | public class PersonController : Controller 9 | { 10 | // GET: api/values 11 | [HttpGet] 12 | [ResponseCache(VaryByHeader = "User-Agent", Duration = 30)] 13 | public IActionResult Get() 14 | { 15 | //Generate demo list using GenFu package 16 | //Returns 200 counts of Person object 17 | var personlist = A.ListOf(200); 18 | return Ok(personlist); 19 | } 20 | } 21 | 22 | public class Person 23 | { 24 | public int Id { get; set; } 25 | public string FirstName { get; set; } 26 | public string LastName { get; set; } 27 | public int Age { get; set; } 28 | public DateTime DoB { get; set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /compression-cache-demo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace compression_cache_demo 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /compression-cache-demo/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:54356/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "compression_cache_demo": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:54357/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /compression-cache-demo/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 11 - Optimization & Performance 12 | 13 | ### compression-cache-demo Project 14 | 15 | Demo code for below feature 16 | 17 | * ResponseCompression 18 | * Response Caching strategies 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /compression-cache-demo/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace compression_cache_demo 6 | { 7 | public class Startup 8 | { 9 | // This method gets called by the runtime. Use this method to add services to the container. 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | services.AddResponseCompression(); 13 | //Response Caching Middleware 14 | services.AddResponseCaching(); 15 | services.AddMvc(); 16 | } 17 | 18 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 19 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 20 | { 21 | app.UseResponseCompression(); 22 | app.UseResponseCaching(); 23 | app.UseMvc(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /compression-cache-demo/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /compression-cache-demo/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /compression-cache-demo/compression-cache-demo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-compression_cache_demo-38016EC0-669A-43DF-963F-8A8CDB140659 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /custom-middleware/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace custom_middleware.Controllers 8 | { 9 | [Route("api/[controller]")] 10 | public class ValuesController : Controller 11 | { 12 | // GET api/values 13 | [HttpGet] 14 | public IEnumerable Get() 15 | { 16 | return new string[] { "value1", "value2" }; 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value"; 24 | } 25 | 26 | // POST api/values 27 | [HttpPost] 28 | public void Post([FromBody]string value) 29 | { 30 | } 31 | 32 | // PUT api/values/5 33 | [HttpPut("{id}")] 34 | public void Put(int id, [FromBody]string value) 35 | { 36 | } 37 | 38 | // DELETE api/values/5 39 | [HttpDelete("{id}")] 40 | public void Delete(int id) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /custom-middleware/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace custom_middleware 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /custom-middleware/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:65039/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "custom_middleware": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:65040/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /custom-middleware/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 6 - Middleware & Filters 11 | 12 | ### Custom-middleware Project 13 | 14 | Demo code for below feature 15 | 16 | * Writing custom middleware 17 | 18 | 19 | Use **Postman** Rest client to test 20 | -------------------------------------------------------------------------------- /custom-middleware/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace custom_middleware 6 | { 7 | public class Startup 8 | { 9 | // This method gets called by the runtime. Use this method to add services to the container. 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | services.AddMvc(); 13 | } 14 | 15 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 16 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 17 | { 18 | //Custom middleware added to pipeline 19 | app.UsePacktHeaderValidator(); 20 | app.UseMvc(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /custom-middleware/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /custom-middleware/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /custom-middleware/custom-middleware.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-custom_middleware-1D831254-EA14-411F-B201-A8386763AEE3 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /distributed-cache-demo/Controllers/Rates.cs: -------------------------------------------------------------------------------- 1 | namespace distributed_cache_demo.Controllers 2 | { 3 | public class Rates 4 | { 5 | public double CAD { get; set; } 6 | public double GBP { get; set; } 7 | public double INR { get; set; } 8 | } 9 | 10 | public class RatesRoot 11 | { 12 | public string @base { get; set; } 13 | public string date { get; set; } 14 | public Rates rates { get; set; } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /distributed-cache-demo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace distributed_cache_demo 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /distributed-cache-demo/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:54965/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "distributed_cache_demo": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:54966/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /distributed-cache-demo/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | * Sql Server 2014 Express Edition 10 | 11 | ## Chapter 11 - Optimization & Performance 12 | 13 | ### distributed-cache-demo Project 14 | 15 | Demo code for below feature 16 | 17 | * Distributed Cache demo 18 | 19 | 20 | 21 | Use **Postman** Rest client to test 22 | -------------------------------------------------------------------------------- /distributed-cache-demo/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /distributed-cache-demo/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /distributed-cache-demo/distributed-cache-demo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-distributed_cache_demo-6BE81920-DB71-49E0-B113-4A55FF5239A8 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /docker-compose.ci.build.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | -------------------------------------------------------------------------------- /docker-compose.dcproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 2.0 5 | Windows 6 | 5b1981ea-987c-48f4-9f6f-39389e3ee15f 7 | True 8 | http://{ServiceIPAddress}/api/values 9 | packtcontacts 10 | 11 | 12 | 13 | 14 | docker-compose.yml 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /docker-compose.override.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | packtcontacts: 5 | environment: 6 | - ASPNETCORE_ENVIRONMENT=Development 7 | ports: 8 | - "80" 9 | networks: 10 | default: 11 | external: 12 | name: nat 13 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | packtcontacts: 5 | image: packtcontacts 6 | build: 7 | context: ./PacktContacts 8 | dockerfile: Dockerfile 9 | -------------------------------------------------------------------------------- /filters-demo/CheckPubliserName.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using Microsoft.AspNetCore.Mvc.Filters; 3 | 4 | namespace filters_demo 5 | { 6 | //Action Filter example 7 | public class CheckPubliserNameAttribute : TypeFilterAttribute 8 | { 9 | public CheckPubliserNameAttribute() : base(typeof 10 | (CheckPubliserName)) 11 | { 12 | } 13 | } 14 | public class CheckPubliserName : IActionFilter 15 | { 16 | public void OnActionExecuted(ActionExecutedContext context) 17 | { 18 | // You can work with Action Result 19 | } 20 | 21 | public void OnActionExecuting(ActionExecutingContext context) 22 | { 23 | var headerValue = context.HttpContext.Request.Headers["publiser-name"]; 24 | if (!headerValue.Equals("Packt")) 25 | { 26 | context.Result = new BadRequestObjectResult("Invalid Header"); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /filters-demo/Controllers/EmployeeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | 3 | namespace filters_demo.Controllers 4 | { 5 | [Route("api/[controller]")] 6 | 7 | public class EmployeeController : Controller 8 | { 9 | // GET api/values/5 10 | [HttpGet("{id}")] 11 | public string Get(int id) 12 | { 13 | if (id == 0) 14 | { 15 | throw new ZeroValueException("Employee Id Cannot be Zero"); 16 | } 17 | return "value is " + id; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /filters-demo/Controllers/ProductsController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Authorization; 2 | using Microsoft.AspNetCore.Mvc; 3 | using System; 4 | 5 | namespace filters_demo.Controllers 6 | { 7 | [Route("api/[controller]")] 8 | [Authorize] 9 | public class ProductsController : Controller 10 | { 11 | // GET: api/values 12 | [HttpGet] 13 | [AllowAnonymous] 14 | public string Get() 15 | { 16 | return "Year is " + DateTime.Now.Year.ToString(); 17 | } 18 | 19 | // GET api/values/5 20 | [HttpGet("{id}")] 21 | public string Get(int id) 22 | { 23 | return "value is " + id; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /filters-demo/PacktExceptionFilter.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Http; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.AspNetCore.Mvc.Filters; 4 | using System; 5 | using System.Net; 6 | 7 | namespace filters_demo 8 | { 9 | // Exception Filter example 10 | public class PacktExceptionFilter : IExceptionFilter 11 | { 12 | public void OnException(ExceptionContext context) 13 | { 14 | HttpStatusCode status = HttpStatusCode.InternalServerError; 15 | String message = String.Empty; 16 | 17 | var exceptionType = context.Exception.GetType(); 18 | 19 | if (exceptionType == typeof(ZeroValueException)) 20 | { 21 | message = context.Exception.Message; 22 | status = HttpStatusCode.InternalServerError; 23 | } 24 | context.Result = new JsonResult(new 25 | { 26 | Code = (int)status, 27 | Message = message 28 | }); 29 | 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /filters-demo/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore; 2 | using Microsoft.AspNetCore.Hosting; 3 | 4 | namespace filters_demo 5 | { 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BuildWebHost(args).Run(); 11 | } 12 | 13 | public static IWebHost BuildWebHost(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseStartup() 16 | .Build(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /filters-demo/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:65264/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "filters_demo": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:65265/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /filters-demo/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 6 - Middleware & Filters 11 | 12 | ### Filters-Demo Project 13 | 14 | Demo code for below feature 15 | 16 | * Inbuilt Filters 17 | * Writing Custom Filters 18 | 19 | 20 | Use **Postman** Rest client to test 21 | -------------------------------------------------------------------------------- /filters-demo/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace filters_demo 6 | { 7 | public class Startup 8 | { 9 | // This method gets called by the runtime. Use this method to add services to the container. 10 | public void ConfigureServices(IServiceCollection services) 11 | { 12 | // Exception global filter added 13 | services.AddMvc(config => { 14 | config.Filters.Add(typeof(PacktExceptionFilter)); 15 | 16 | services.AddScoped(); 17 | }); 18 | } 19 | 20 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 21 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 22 | { 23 | app.UseMvc(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /filters-demo/ZeroValueException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace filters_demo 4 | { 5 | public class ZeroValueException : Exception 6 | { 7 | public ZeroValueException(){} 8 | 9 | public ZeroValueException(string message) 10 | : base(message) 11 | { } 12 | 13 | public ZeroValueException(string message, Exception innerException) 14 | : base(message, innerException) 15 | { } 16 | } 17 | } -------------------------------------------------------------------------------- /filters-demo/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /filters-demo/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /filters-demo/filters-demo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-filters_demo-606FCCA7-2633-4E79-B09D-2ACE4E3CA0E1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace httpmodule_to_middleware 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:65254/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "httpmodule_to_middleware": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:65255/" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 6 - Middleware & Filters 11 | 12 | ### httpmodule-to-middleware Project 13 | 14 | Demo code for below feature 15 | 16 | * Converting leagacy HTTP Modules to ASP.NET Core Middleware 17 | 18 | 19 | Use **Postman** Rest client to test 20 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.AspNetCore.Http; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace httpmodule_to_middleware 7 | { 8 | public class Startup 9 | { 10 | // This method gets called by the runtime. Use this method to add services to the container. 11 | public void ConfigureServices(IServiceCollection services) 12 | { 13 | services.AddMvc(); 14 | } 15 | 16 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 17 | public void Configure(IApplicationBuilder app, IHostingEnvironment env) 18 | { 19 | // Calling HTTP module migrated to middleware 20 | app.UseAspxMiddleware(); 21 | app.Run(async (context) => 22 | { 23 | await context.Response.WriteAsync("Hello World!"); 24 | }); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "Debug": { 5 | "LogLevel": { 6 | "Default": "Warning" 7 | } 8 | }, 9 | "Console": { 10 | "LogLevel": { 11 | "Default": "Warning" 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /httpmodule-to-middleware/httpmodule-to-middleware.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | aspnet-httpmodule_to_middleware-9D63CE16-B03E-4111-8A24-1A64BE895373 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /middleware-basics/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace middleware_basics 12 | { 13 | public class Program 14 | { 15 | public static void Main(string[] args) 16 | { 17 | BuildWebHost(args).Run(); 18 | } 19 | 20 | public static IWebHost BuildWebHost(string[] args) => 21 | WebHost.CreateDefaultBuilder(args) 22 | .UseStartup() 23 | .Build(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /middleware-basics/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:64913/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "environmentVariables": { 15 | "ASPNETCORE_ENVIRONMENT": "Development" 16 | } 17 | }, 18 | "middleware_basics": { 19 | "commandName": "Project", 20 | "launchBrowser": true, 21 | "environmentVariables": { 22 | "ASPNETCORE_ENVIRONMENT": "Development" 23 | }, 24 | "applicationUrl": "http://localhost:64914/" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /middleware-basics/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * ASP.NET Core 2017 Preview 2 6 | * .NET Core 2 SDK Preview 2 7 | * Visual Studio 2017 Preview 3 or Visual Studio Code 8 | * Postman 9 | 10 | ## Chapter 6 - Middleware & Filters 11 | 12 | ### Middleware-Basics Project 13 | 14 | Demo code for below feature 15 | 16 | * Basics of middleware 17 | 18 | 19 | Use **Postman** Rest client to test 20 | -------------------------------------------------------------------------------- /middleware-basics/middleware-basics.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /ngPacktContacts/.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /ngPacktContacts/.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | testem.log 34 | /typings 35 | 36 | # e2e 37 | /e2e/*.js 38 | /e2e/*.map 39 | 40 | # System Files 41 | .DS_Store 42 | Thumbs.db 43 | -------------------------------------------------------------------------------- /ngPacktContacts/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * Latest NodeJS 6 | * Angular 4 7 | * Visual Studio Code 8 | * Your favourite browser 9 | 10 | ## Chapter 13 - Modern Web Frontends 11 | 12 | ### ngPacktContacts Project 13 | 14 | Demo for consuming *PacktContacts* Web API created in Chapter 12 15 | 16 | * Consuming using Angular 4 17 | 18 | ### How to run? 19 | * Installed latest NodeJS 20 | * Run the `npm install` from root folder containing package.json. This will install all Node packages 21 | * **PacktContacts** Web API should be either hosted on IIS or running locally. 22 | * Update Web API endpoint URL in Angular project 23 | * Once done, run the `ng serve` to run Angular project 24 | * Open browser, navigate to http://localhost:3000 to view project in action 25 | 26 | 27 | -------------------------------------------------------------------------------- /ngPacktContacts/e2e/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { NgPacktContactsPage } from './app.po'; 2 | 3 | describe('ng-packt-contacts App', () => { 4 | let page: NgPacktContactsPage; 5 | 6 | beforeEach(() => { 7 | page = new NgPacktContactsPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('Welcome to app!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /ngPacktContacts/e2e/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class NgPacktContactsPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ngPacktContacts/e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/e2e", 5 | "baseUrl": "./", 6 | "module": "commonjs", 7 | "target": "es5", 8 | "types": [ 9 | "jasmine", 10 | "jasminewd2", 11 | "node" 12 | ] 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ngPacktContacts/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/0.13/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular/cli'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular/cli/plugins/karma') 14 | ], 15 | client:{ 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | reports: [ 'html', 'lcovonly' ], 20 | fixWebpackSourcePaths: true 21 | }, 22 | angularCli: { 23 | environment: 'dev' 24 | }, 25 | reporters: ['progress', 'kjhtml'], 26 | port: 9876, 27 | colors: true, 28 | logLevel: config.LOG_INFO, 29 | autoWatch: true, 30 | browsers: ['Chrome'], 31 | singleRun: false 32 | }); 33 | }; 34 | -------------------------------------------------------------------------------- /ngPacktContacts/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './e2e/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: 'e2e/tsconfig.e2e.json' 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/app.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/ngPacktContacts/src/app/app.component.css -------------------------------------------------------------------------------- /ngPacktContacts/src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.css'] 7 | }) 8 | export class AppComponent { 9 | title = 'app'; 10 | } 11 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { HttpModule } from '@angular/http'; 2 | import { PacktAPIsService } from './packt-apis.service'; 3 | import { PacktRoutesModule } from './packt-routes/packt-routes.module'; 4 | import { BrowserModule } from '@angular/platform-browser'; 5 | import { NgModule } from '@angular/core'; 6 | 7 | import { AppComponent } from './app.component'; 8 | import { ReactiveFormsModule } from '@angular/forms'; 9 | import { LoginComponent } from './login/login.component'; 10 | import { ContactsComponent } from './contacts/contacts.component'; 11 | 12 | @NgModule({ 13 | declarations: [ 14 | AppComponent, 15 | LoginComponent, 16 | ContactsComponent 17 | ], 18 | imports: [ 19 | BrowserModule, 20 | ReactiveFormsModule, 21 | HttpModule, 22 | PacktRoutesModule 23 | ], 24 | providers: [PacktAPIsService], 25 | bootstrap: [AppComponent] 26 | }) 27 | export class AppModule { } 28 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/contacts.class.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Contacts 3 | */ 4 | export class Contacts { 5 | id:number; 6 | firstName:string; 7 | lastName:string; 8 | email:string; 9 | dateOfBirth:Date; 10 | } -------------------------------------------------------------------------------- /ngPacktContacts/src/app/contacts/contacts.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/ngPacktContacts/src/app/contacts/contacts.component.css -------------------------------------------------------------------------------- /ngPacktContacts/src/app/contacts/contacts.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ContactsComponent } from './contacts.component'; 4 | 5 | describe('ContactsComponent', () => { 6 | let component: ContactsComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ContactsComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ContactsComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should be created', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/credentials-model.ts: -------------------------------------------------------------------------------- 1 | export class CredentialsModel { 2 | Username: string; 3 | Password: string; 4 | } 5 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/login/login.component.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/ngPacktContacts/src/app/login/login.component.css -------------------------------------------------------------------------------- /ngPacktContacts/src/app/login/login.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | 12 |
13 |
14 |
15 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/login/login.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { LoginComponent } from './login.component'; 4 | 5 | describe('LoginComponent', () => { 6 | let component: LoginComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ LoginComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(LoginComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should be created', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/packt-apis.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, inject } from '@angular/core/testing'; 2 | 3 | import { PacktAPIsService } from './packt-apis.service'; 4 | 5 | describe('PacktAPIsService', () => { 6 | beforeEach(() => { 7 | TestBed.configureTestingModule({ 8 | providers: [PacktAPIsService] 9 | }); 10 | }); 11 | 12 | it('should be created', inject([PacktAPIsService], (service: PacktAPIsService) => { 13 | expect(service).toBeTruthy(); 14 | })); 15 | }); 16 | -------------------------------------------------------------------------------- /ngPacktContacts/src/app/packt-routes/packt-routes.module.ts: -------------------------------------------------------------------------------- 1 | import { ContactsComponent } from './../contacts/contacts.component'; 2 | import { LoginComponent } from './../login/login.component'; 3 | import { NgModule } from '@angular/core'; 4 | import { CommonModule } from '@angular/common'; 5 | import { RouterModule, Routes } from '@angular/router'; 6 | 7 | const routes: Routes = [ 8 | { 9 | path: '', 10 | component: LoginComponent, 11 | }, 12 | { 13 | path: 'contacts', 14 | component: ContactsComponent, 15 | } 16 | ]; 17 | @NgModule({ 18 | imports: [ 19 | CommonModule, 20 | RouterModule.forRoot(routes) 21 | ], 22 | exports: [ 23 | RouterModule 24 | ], 25 | declarations: [] 26 | }) 27 | 28 | export class PacktRoutesModule { } 29 | -------------------------------------------------------------------------------- /ngPacktContacts/src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/ngPacktContacts/src/assets/.gitkeep -------------------------------------------------------------------------------- /ngPacktContacts/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /ngPacktContacts/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // The file contents for the current environment will overwrite these during build. 2 | // The build system defaults to the dev environment which uses `environment.ts`, but if you do 3 | // `ng build --env=prod` then `environment.prod.ts` will be used instead. 4 | // The list of which env maps to which file can be found in `.angular-cli.json`. 5 | 6 | export const environment = { 7 | production: false 8 | }; 9 | -------------------------------------------------------------------------------- /ngPacktContacts/src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/ngPacktContacts/src/favicon.ico -------------------------------------------------------------------------------- /ngPacktContacts/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NgPacktContacts 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /ngPacktContacts/src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule); 12 | -------------------------------------------------------------------------------- /ngPacktContacts/src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "baseUrl": "./", 6 | "module": "es2015", 7 | "types": [] 8 | }, 9 | "exclude": [ 10 | "test.ts", 11 | "**/*.spec.ts" 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /ngPacktContacts/src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "baseUrl": "./", 6 | "module": "commonjs", 7 | "target": "es5", 8 | "types": [ 9 | "jasmine", 10 | "node" 11 | ] 12 | }, 13 | "files": [ 14 | "test.ts" 15 | ], 16 | "include": [ 17 | "**/*.spec.ts", 18 | "**/*.d.ts" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /ngPacktContacts/src/typings.d.ts: -------------------------------------------------------------------------------- 1 | /* SystemJS module definition */ 2 | declare var module: NodeModule; 3 | interface NodeModule { 4 | id: string; 5 | } 6 | -------------------------------------------------------------------------------- /ngPacktContacts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "outDir": "./dist/out-tsc", 5 | "sourceMap": true, 6 | "declaration": false, 7 | "moduleResolution": "node", 8 | "emitDecoratorMetadata": true, 9 | "experimentalDecorators": true, 10 | "target": "es5", 11 | "typeRoots": [ 12 | "node_modules/@types" 13 | ], 14 | "lib": [ 15 | "es2016", 16 | "dom" 17 | ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /packtcontactapp/.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs 2 | # editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | indent_style = space 8 | indent_size = 2 9 | 10 | # We recommend you to keep these unchanged 11 | end_of_line = lf 12 | charset = utf-8 13 | trim_trailing_whitespace = true 14 | insert_final_newline = true 15 | 16 | [*.md] 17 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /packtcontactapp/.gitignore: -------------------------------------------------------------------------------- 1 | # Specifies intentionally untracked files to ignore when using Git 2 | # http://git-scm.com/docs/gitignore 3 | 4 | *~ 5 | *.sw[mnpcod] 6 | *.log 7 | *.tmp 8 | *.tmp.* 9 | log.txt 10 | *.sublime-project 11 | *.sublime-workspace 12 | .vscode/ 13 | npm-debug.log* 14 | 15 | .idea/ 16 | .sass-cache/ 17 | .tmp/ 18 | .versions/ 19 | coverage/ 20 | dist/ 21 | node_modules/ 22 | tmp/ 23 | temp/ 24 | hooks/ 25 | platforms/ 26 | plugins/ 27 | plugins/android.json 28 | plugins/ios.json 29 | www/ 30 | $RECYCLE.BIN/ 31 | 32 | .DS_Store 33 | Thumbs.db 34 | UserInterfaceState.xcuserstate 35 | -------------------------------------------------------------------------------- /packtcontactapp/README.md: -------------------------------------------------------------------------------- 1 | # Mastering ASP.NET Web API 2 | 3 | Softwares required 4 | 5 | * Latest NodeJS 6 | * Ionic 3 7 | * Visual Studio Code 8 | * Your favourite browser 9 | 10 | ## Chapter 13 - Modern Web Frontends 11 | 12 | ### packtcontactapp Project 13 | 14 | Demo for consuming *PacktContacts* Web API created in Chapter 12 15 | 16 | * Consuming using Ionic 3 (Hybrid Mobile App) 17 | 18 | ### How to run? 19 | * Installed latest NodeJS 20 | * Run the `npm install` from root folder containing package.json. This will install all Node packages 21 | * **PacktContacts** Web API should be either hosted on IIS or running locally. 22 | * Update Web API endpoint URL in Ionic project 23 | * Run the command `ionic serve`. It automatically opens browser to start application 24 | 25 | -------------------------------------------------------------------------------- /packtcontactapp/ionic.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "packtcontactapp", 3 | "app_id": "", 4 | "type": "ionic-angular" 5 | } 6 | -------------------------------------------------------------------------------- /packtcontactapp/resources/android/icon/drawable-hdpi-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/icon/drawable-hdpi-icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/icon/drawable-ldpi-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/icon/drawable-ldpi-icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/icon/drawable-mdpi-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/icon/drawable-mdpi-icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/icon/drawable-xhdpi-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/icon/drawable-xhdpi-icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/icon/drawable-xxhdpi-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/icon/drawable-xxhdpi-icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/icon/drawable-xxxhdpi-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/icon/drawable-xxxhdpi-icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-land-hdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-land-hdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-land-ldpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-land-ldpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-land-mdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-land-mdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-land-xhdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-land-xhdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-land-xxhdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-land-xxhdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-land-xxxhdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-land-xxxhdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-port-hdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-port-hdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-port-ldpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-port-ldpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-port-mdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-port-mdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-port-xhdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-port-xhdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-port-xxhdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-port-xxhdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/android/splash/drawable-port-xxxhdpi-screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/android/splash/drawable-port-xxxhdpi-screen.png -------------------------------------------------------------------------------- /packtcontactapp/resources/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-40.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-40.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-40@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-40@3x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-50.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-50@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-50@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-60.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-60@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-60@3x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-72.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-72@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-72@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-76.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-76@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-83.5@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-small.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-small@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-small@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon-small@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon-small@3x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/icon/icon@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/icon/icon@2x.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-568h@2x~iphone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-568h@2x~iphone.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-667h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-667h.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-736h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-736h.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Landscape-736h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Landscape-736h.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Landscape@2x~ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Landscape@2x~ipad.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Landscape@~ipadpro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Landscape@~ipadpro.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Landscape~ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Landscape~ipad.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Portrait@2x~ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Portrait@2x~ipad.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Portrait@~ipadpro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Portrait@~ipadpro.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default-Portrait~ipad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default-Portrait~ipad.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default@2x~iphone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default@2x~iphone.png -------------------------------------------------------------------------------- /packtcontactapp/resources/ios/splash/Default~iphone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/ios/splash/Default~iphone.png -------------------------------------------------------------------------------- /packtcontactapp/resources/splash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/resources/splash.png -------------------------------------------------------------------------------- /packtcontactapp/src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { LoginPage } from './../pages/login/login'; 2 | import { Component } from '@angular/core'; 3 | import { Platform } from 'ionic-angular'; 4 | import { StatusBar } from '@ionic-native/status-bar'; 5 | import { SplashScreen } from '@ionic-native/splash-screen'; 6 | 7 | import { HomePage } from '../pages/home/home'; 8 | @Component({ 9 | templateUrl: 'app.html' 10 | }) 11 | export class MyApp { 12 | rootPage:any = LoginPage; 13 | 14 | constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) { 15 | platform.ready().then(() => { 16 | // Okay, so the platform is ready and our plugins are available. 17 | // Here you can do any higher level native things you might need. 18 | statusBar.styleDefault(); 19 | splashScreen.hide(); 20 | }); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /packtcontactapp/src/app/app.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /packtcontactapp/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { PacktService } from './../providers/packt-service'; 2 | import { LoginPage } from './../pages/login/login'; 3 | import { BrowserModule } from '@angular/platform-browser'; 4 | import { ErrorHandler, NgModule } from '@angular/core'; 5 | import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; 6 | import { SplashScreen } from '@ionic-native/splash-screen'; 7 | import { StatusBar } from '@ionic-native/status-bar'; 8 | 9 | import { MyApp } from './app.component'; 10 | import { HomePage } from '../pages/home/home'; 11 | import { HttpModule } from "@angular/http"; 12 | 13 | @NgModule({ 14 | declarations: [ 15 | MyApp, 16 | HomePage, LoginPage 17 | ], 18 | imports: [ 19 | BrowserModule, 20 | HttpModule, 21 | IonicModule.forRoot(MyApp) 22 | ], 23 | bootstrap: [IonicApp], 24 | entryComponents: [ 25 | MyApp, 26 | HomePage, 27 | LoginPage 28 | ], 29 | providers: [ 30 | StatusBar, 31 | SplashScreen, PacktService, 32 | {provide: ErrorHandler, useClass: IonicErrorHandler} 33 | ] 34 | }) 35 | export class AppModule {} 36 | -------------------------------------------------------------------------------- /packtcontactapp/src/app/app.scss: -------------------------------------------------------------------------------- 1 | // http://ionicframework.com/docs/theming/ 2 | 3 | 4 | // App Global Sass 5 | // -------------------------------------------------- 6 | // Put style rules here that you want to apply globally. These 7 | // styles are for the entire app and not just one component. 8 | // Additionally, this file can be also used as an entry point 9 | // to import other Sass files to be included in the output CSS. 10 | // 11 | // Shared Sass variables, which can be used to adjust Ionic's 12 | // default Sass variables, belong in "theme/variables.scss". 13 | // 14 | // To declare rules for a specific mode, create a child rule 15 | // for the .md, .ios, or .wp mode classes. The mode class is 16 | // automatically applied to the element in the app. 17 | -------------------------------------------------------------------------------- /packtcontactapp/src/app/main.ts: -------------------------------------------------------------------------------- 1 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 2 | 3 | import { AppModule } from './app.module'; 4 | 5 | platformBrowserDynamic().bootstrapModule(AppModule); 6 | -------------------------------------------------------------------------------- /packtcontactapp/src/assets/icon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Mastering-ASP.NET-Web-API/f971248228efba45e09bccbc07b1c6f391bc3049/packtcontactapp/src/assets/icon/favicon.ico -------------------------------------------------------------------------------- /packtcontactapp/src/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Ionic", 3 | "short_name": "Ionic", 4 | "start_url": "index.html", 5 | "display": "standalone", 6 | "icons": [{ 7 | "src": "assets/imgs/logo.png", 8 | "sizes": "512x512", 9 | "type": "image/png" 10 | }], 11 | "background_color": "#4e8ef7", 12 | "theme_color": "#4e8ef7" 13 | } -------------------------------------------------------------------------------- /packtcontactapp/src/pages/home/home.scss: -------------------------------------------------------------------------------- 1 | page-home { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /packtcontactapp/src/pages/login/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Packt Contacts App 5 | 6 | 7 | 8 | 9 |
10 |
11 | 12 | 13 | Username 14 | 15 | 16 | 17 | Password 18 | 19 | 20 |
21 | 22 |
23 |
24 |
25 |
26 |
27 | -------------------------------------------------------------------------------- /packtcontactapp/src/pages/login/login.scss: -------------------------------------------------------------------------------- 1 | page-login { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /packtcontactapp/src/providers/contacts.class.ts: -------------------------------------------------------------------------------- 1 | export class Contacts { 2 | id:number; 3 | firstName:string; 4 | lastName:string; 5 | email:string; 6 | } 7 | -------------------------------------------------------------------------------- /packtcontactapp/src/service-worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Check out https://googlechrome.github.io/sw-toolbox/ for 3 | * more info on how to use sw-toolbox to custom configure your service worker. 4 | */ 5 | 6 | 7 | 'use strict'; 8 | importScripts('./build/sw-toolbox.js'); 9 | 10 | self.toolbox.options.cache = { 11 | name: 'ionic-cache' 12 | }; 13 | 14 | // pre-cache our key assets 15 | self.toolbox.precache( 16 | [ 17 | './build/main.js', 18 | './build/vendor.js', 19 | './build/main.css', 20 | './build/polyfills.js', 21 | 'index.html', 22 | 'manifest.json' 23 | ] 24 | ); 25 | 26 | // dynamically cache any other local assets 27 | self.toolbox.router.any('/*', self.toolbox.cacheFirst); 28 | 29 | // for any other requests go to the network, cache, 30 | // and then only use that cached resource if your user goes offline 31 | self.toolbox.router.default = self.toolbox.networkFirst; 32 | -------------------------------------------------------------------------------- /packtcontactapp/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowSyntheticDefaultImports": true, 4 | "declaration": false, 5 | "emitDecoratorMetadata": true, 6 | "experimentalDecorators": true, 7 | "lib": [ 8 | "dom", 9 | "es2015" 10 | ], 11 | "module": "es2015", 12 | "moduleResolution": "node", 13 | "sourceMap": true, 14 | "target": "es5" 15 | }, 16 | "include": [ 17 | "src/**/*.ts" 18 | ], 19 | "exclude": [ 20 | "node_modules" 21 | ], 22 | "compileOnSave": false, 23 | "atom": { 24 | "rewriteTsconfig": false 25 | } 26 | } -------------------------------------------------------------------------------- /packtcontactapp/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "no-duplicate-variable": true, 4 | "no-unused-variable": [ 5 | true 6 | ] 7 | }, 8 | "rulesDirectory": [ 9 | "node_modules/tslint-eslint-rules/dist/rules" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /packtcontactapp/typings/cordova-typings.d.ts: -------------------------------------------------------------------------------- 1 | 2 | /// 3 | /// 4 | /// -------------------------------------------------------------------------------- /web/ReadMe.txt: -------------------------------------------------------------------------------- 1 | WebDemoAPP1 & WebDemoAPP2 used to call Web API using AJAX. 2 | 3 | How to run 4 | 1. npm install from command line (NodeJS should be installed) 5 | 2. npm run dev to start the web apps 6 | 7 | 8 | However WebDemoAPP2 is not present in Origin policy of Web API. 9 | -------------------------------------------------------------------------------- /web/WebDemoAPP1/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Web Demo App1 4 | 5 | 6 |

Web Demo App1

7 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /web/WebDemoAPP1/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "demoapp1", 3 | "author": "Mithun Pattankar", 4 | "description": "a simple web app running using lite-server", 5 | "license": "MIT", 6 | "scripts": { 7 | "dev": "lite-server" 8 | }, 9 | "dependencies": { 10 | "lite-server": "^2.3.0" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /web/WebDemoAPP2/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Web Demo App2 4 | 5 | 6 |

Web Demo App2

7 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /web/WebDemoAPP2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "demoapp1", 3 | "author": "Mithun Pattankar", 4 | "description": "a simple web app running using lite-server", 5 | "license": "MIT", 6 | "scripts": { 7 | "dev": "lite-server" 8 | }, 9 | "dependencies": { 10 | "lite-server": "^2.3.0" 11 | } 12 | } 13 | --------------------------------------------------------------------------------