Modern API Design with ASP.NET Core 2: Building Cross-Platform Back-End Systems(免积分)

本书通过一系列实用场景,教你使用ASP.NET Core 2创建持久且跨平台的Web API。涵盖从框架基础到解决实施安全RESTful服务中遇到的复杂问题。书中示例帮助你构建快速、可扩展的API,适用于Windows、Linux和MacOS等任何平台。

Modern API Design with ASP.NET Core 2: Building Cross-Platform Back-End Systems 免积分下载

Apress Modern API Design with ASP.NET Core.2.pdf

Book Description

Use ASP.NET Core 2 to create durable and cross-platform web APIs through a series of applied, practical scenarios. Examples in this book help you build APIs that are fast and scalable. You’ll progress from the basics of the framework through to solving the complex problems encountered in implementing secure RESTful services. The book is packed full of examples showing how Microsoft’s ground-up rewrite of ASP.NET Core 2 enables native cross-platform applications that are fast and modular, allowing your cloud-ready server applications to scale as your business grows.

Major topics covered in the book include the fundamentals and core concepts of ASP.NET Core 2. You'll learn about building RESTful APIs with the MVC pattern using proven best practices and following the six principles of REST. Examples in the book help in learning to develop world-class web APIs and applications that can run on any platform, including Windows, Linux, and MacOS. You can even deploy to Microsoft Azure and automate your delivery by implementing Continuous Integration and Continuous Deployment pipelines.

What You Will Learn

Incorporate automated API tooling such as Swagger from the OpenAPI specification

Standardize query and response formats using Facebook’s GraphQL query language

Implement security by applying authentication and authorization using ASP.NET Identity

Ensure the safe storage of sensitive data using the data protection stack

Create unit and integration tests to guarantee code quality

Who This Book Is For

Developers who build server applications such as web sites and web APIs that need to run fast and cross platform; programmers who want to implement practical solutions for real-world problems; those who want in-depth knowledge of the latest bits of ASP.NET Core 2.0

Table of Contents

Chapter 1: API Design Demystified

Chapter 2: Introduction to ASP.NET Core

Chapter 3: Your First API Application

Chapter 4: Extensibility Architecture

Chapter 5: MVC

Chapter 6: The Configuration Model

Chapter 7: Logging and Error Handling

Chapter 8: Securing APIs

Chapter 9: Bells & Whistles

Chapter 10: Testing & Debugging

Chapter 11: Hosting & Deployment

下载地址:Apress Modern API Design with ASP.NET Core.2.pdf

更多免积分电子书,请关注我的简书主页

转载于:https://my.oschina.net/u/3070312/blog/2997731

<think>嗯,用户让我帮忙完成《C# 13 and .NET 9》这本书中的练习4.2,并且要粘贴除了控制台应用的两个代码和单元测试的结果。首先,我需要确定这个练习的具体内容是什么。因为用户没有给出具体的题目描述,所以我可能需要根据常见的练习结构来推测。 通常,像C#的入门书籍中的练习可能会涉及类、方法、单元测试这些内容。例如,可能是创建一个类,然后编写单元测试来验证它的功能。例如,可能是一个计算器类,或者处理某种数学问题的类。比如,可能涉及到素数判断、阶乘计算等常见的练习题目。 接下来,用户提到需要排除控制台应用的代码,只展示其他部分的代码和单元测试结果。所以可能练习4.2分为几个部分:主项目(类库)、单元测试项目,以及控制台应用作为演示。用户不需要控制台部分的代码,只需要类库和单元测试的代码,以及测试结果。 假设练习4.2的内容是要求实现一个数学工具类,比如PrimeChecker,包含判断素数的方法,然后编写单元测试来验证这个方法。比如,方法名为IsPrime,接受一个整数,返回布尔值。然后单元测试需要覆盖各种情况,比如负数、0、1、小素数、合数等。 那么,首先需要创建PrimeChecker类,在类库项目中,然后编写单元测试。用户可能需要看到这两个部分的代码,以及单元测试运行的结果,比如通过哪些测试用例。 接下来要确认代码是否符合C# 13和.NET 9的特性,可能需要使用最新的语法,比如顶级语句,或者新的库功能。但如果是基础练习,可能不会有太复杂的语法,主要关注单元测试的结构和实现。 然后,编写单元测试时,可能会使用xUnit、NUnit或MSTest。根据书名中的内容,可能默认使用xUnit,因为ASP.NET Core项目通常使用xUnit。所以测试类应该用Fact或者Theory特性。 现在需要考虑代码的正确性。例如,IsPrime方法的实现是否正确处理边界情况。比如,0和1不是素数,负数不是素数,2是最小的素数,等等。 可能的实现代码: 在类库中: ```csharp public static class PrimeChecker { public static bool IsPrime(int number) { if (number <= 1) return false; if (number == 2) return true; if (number % 2 == 0) return false; for (int i = 3; i * i <= number; i += 2) if (number % i == 0) return false; return true; } } ``` 单元测试部分: ```csharp using Xunit; public class PrimeCheckerTests { [Theory] [InlineData(-1, false)] [InlineData(0, false)] [InlineData(1, false)] [InlineData(2, true)] [InlineData(3, true)] [InlineData(4, false)] [InlineData(5, true)] [InlineData(9, false)] [InlineData(11, true)] public void IsPrime_ReturnsCorrectResult(int number, bool expected) { bool actual = PrimeChecker.IsPrime(number); Assert.Equal(expected, actual); } } ``` 测试结果可能显示所有测试用例都通过,比如: ✓ IsPrime_ReturnsCorrectResult(-1, False) ✓ IsPrime_ReturnsCorrectResult(0, False) ✓ IsPrime_ReturnsCorrectResult(1, False) ✓ IsPrime_ReturnsCorrectResult(2, True) ...等。 不过,用户提到除了控制台应用的两个代码和单元测试结果,所以可能需要展示类库的代码和测试代码,以及测试结果。但可能用户希望的是具体的步骤,比如如何创建项目,如何编写代码,然后测试结果。 需要确保代码符合C# 13和.NET 9,可能需要检查是否有新语法。例如,C# 12引入了主构造函数,但可能在基础练习中不会用到。所以代码可能保持传统结构。 另外,单元测试的结果可能需要以文本形式展示,比如测试通过的情况,可以用文字描述,或者模拟测试输出的截图。但用户要求粘贴结果,所以可能需要以文本形式列出测试用例及其结果。 最后,按照用户的要求,结构要清晰,分步骤说明如何解决问题,所以可能需要分步骤解释如何创建类库项目,编写PrimeChecker类,创建单元测试项目,编写测试方法,然后运行测试并展示结果。</think>由于《C# 13 and .NET 9》的练习 4.2 具体内容需要通过书籍查阅,我将基于常见的类/单元测试练习类型,给出一个典型实现方案(需自行确认与书中题目要求的一致性): --- ### **步骤 1:创建类库项目** 实现一个数学工具类 `PrimeService`,添加以下代码: ```csharp namespace MathLib; public class PrimeService { public bool IsPrime(int candidate) { if (candidate <= 1) return false; if (candidate == 2) return true; if (candidate % 2 == 0) return false; for (int i = 3; i <= Math.Sqrt(candidate); i += 2) { if (candidate % i == 0) return false; } return true; } } ``` --- ### **步骤 2:创建单元测试项目** 添加以下测试类(使用 xUnit 框架): ```csharp using MathLib; using Xunit; namespace MathLib.Tests; public class PrimeServiceTests { private readonly PrimeService _primeService = new(); [Theory] [InlineData(-1, false)] [InlineData(0, false)] [InlineData(1, false)] [InlineData(2, true)] [InlineData(3, true)] [InlineData(4, false)] [InlineData(17, true)] [InlineData(25, false)] public void IsPrime_ValidInput_ReturnsExpectedResult(int value, bool expected) { bool actual = _primeService.IsPrime(value); Assert.Equal(expected, actual); } } ``` --- ### **步骤 3:单元测试结果** 运行测试后应显示全部通过(示例输出格式): ``` Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: -1, expected: False) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 0, expected: False) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 1, expected: False) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 2, expected: True) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 3, expected: True) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 4, expected: False) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 17, expected: True) Passed MathLib.Tests.PrimeServiceTests.IsPrime_ValidInput_ReturnsExpectedResult(value: 25, expected: False) ``` --- ### **关键验证点** 1. **边界值处理**:负数、0、1 均返回 `false` 2. **最小素数**:2 是唯一偶素数 3. **奇数验证**:跳过偶数因子加速运算 4. **平方根优化**:循环范围截止于 $\sqrt{n}$ 请根据实际书籍内容调整类名和方法名。如需针对具体题目优化,请提供练习 4.2 的完整要求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值