ASP.NET_Core_Guide_Notes
ASP.NET_Core_Guide_Notes
ASP.NET Core is a cross-platform framework for building web applications and APIs.
Key components:
- Program.cs: The entry point where the application is configured and started.
Example Controller:
```csharp
```
```html
Page 1
ASP.NET Core Guide Notes
```
```csharp
services.AddTransient<IMyService, MyService>();
```
```csharp
```
4. Routing
Page 2
ASP.NET Core Guide Notes
Routing defines URL patterns and their mappings to controllers and actions.
```csharp
app.UseRouting();
app.UseEndpoints(endpoints =>
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
```
5. Model Binding
Example model:
```csharp
Page 3
ASP.NET Core Guide Notes
```
```csharp
return View();
```
```csharp
[Required]
[Range(18, 120)]
Page 4
ASP.NET Core Guide Notes
```
```csharp
<h1>@Model.Name</h1>
<p>Age: @Model.Age</p>
```
```html
<form asp-action="Submit">
<button type="submit">Submit</button>
</form>
```
Page 5
ASP.NET Core Guide Notes
9. Validation
Example in controller:
```csharp
if (!ModelState.IsValid)
return View(person);
// Proceed if valid
return RedirectToAction("Success");
```
```csharp
Page 6
ASP.NET Core Guide Notes
[CustomValidation(typeof(PersonValidator))]
```
EF Core allows using existing databases to generate models and perform CRUD operations.
Example DbContext:
```csharp
```
Page 7
ASP.NET Core Guide Notes
CRUD operations:
Create:
```csharp
context.SaveChanges();
```
Read:
```csharp
```
Update:
```csharp
context.SaveChanges();
```
Delete:
```csharp
context.Persons.Remove(person);
Page 8
ASP.NET Core Guide Notes
context.SaveChanges();
```
Model Binding and ModelState validation handle incoming data and return appropriate responses.
Example controller:
```csharp
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok(person);
```
Example session:
```csharp
Page 9
ASP.NET Core Guide Notes
HttpContext.Session.SetString("UserName", "JohnDoe");
```
```csharp
Response.Cookies.Append("UserName", "JohnDoe");
```
```csharp
if (file != null)
file.CopyTo(stream);
Page 10
ASP.NET Core Guide Notes
```
Example:
```csharp
```
```csharp
```
Example:
```csharp
Page 11
ASP.NET Core Guide Notes
```
```csharp
[ApiController]
[Route("api/[controller]")]
[HttpGet]
```
Middleware pipeline:
```csharp
app.UseMiddleware<CustomMiddleware>();
app.UseRouting();
Page 12
ASP.NET Core Guide Notes
```
AppSettings configuration:
```json
"Logging": {
"LogLevel": {
"Default": "Information"
```
21. Localization
Localization example:
```csharp
```
Page 13
ASP.NET Core Guide Notes
```csharp
// Custom logic
await _next(context);
```
Authentication:
```csharp
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
```
Authorization:
```csharp
Page 14
ASP.NET Core Guide Notes
services.AddAuthorization(options =>
});
```
Logging:
```csharp
```
25. Caching
MemoryCache example:
```csharp
services.AddMemoryCache();
entry.SlidingExpiration = TimeSpan.FromMinutes(5);
});
```
Page 15
ASP.NET Core Guide Notes
26. Security
```csharp
```
```csharp
try
await _next(context);
context.Response.StatusCode = 500;
Page 16
ASP.NET Core Guide Notes
```
```csharp
services.AddScoped<IMyService, MyService>();
```
Page 17