NET Reference Sheet1
NET Reference Sheet1
•Creating models, controllers, repo and services folders and their le.
•De ning models -> data -> program.cs -> and app settings .
Adding migration -> updating database.
6.
dotnet add package Microsoft.AspNetCore.OpenApi --version 8.0.4
dotnet add package Microsoft.EntityFrameworkCore.Design --version 8.0.4
dotnet add package Pomelo.EntityFrameworkCore.MySql --version 8.0.0
dotnet add package Swashbuckle.AspNetCore --version 8.1.0
dotnet add package Microsoft.EntityFrameworkCore --version 8.0.4
fi
ff
fi
Topic: Routing in ASP.NET Core (.NET 6 and above)
Routing is the backbone of how an ASP.NET Core application responds to HTTP requests. It
de nes how incoming URLs are matched to controllers and actions within your Web API or MVC
app.
1. Conventional Routing
2. Attribute Routing
🔹 1. Conventional Routing
This pattern-based approach de nes routes centrally, often used in MVC-style apps.
Explanation:
Note: This is less common in Web APIs where attribute routing is more explicit and exible.
🔹 2. Attribute Routing
Preferred in Web APIs, attribute routing allows you to de ne routes directly on controllers and
actions using attributes.
Example:
csharp
CopyEdit
[HttpPost]
public IActionResult AddBook([FromBody] Book book)
{
// logic here
}
}
✨ Key Attributes:
fi
fi
fi
fl
• [Route("api/[controller]")] – sets the base route using the controller name (Book → /api/
book)
🔹 3. Route Parameters
• Required: [HttpGet("{id}")]
• Optional: [HttpGet("{id?}")]
Example:
csharp
CopyEdit
[HttpPost]
public IActionResult Create(Product product) { ... }
}
🔹 5. Best Practices
Example:
app.MapGet("/api/books/{id}", (int id) => { ... }); app.MapPost("/api/books", (Book book) =>
{ ... });
🔚 Conclusion
Routing is the bridge between your client requests and the server logic. While conventional routing
works well in MVC, attribute routing is more explicit, exible, and well-suited for APIs.
Understanding route patterns, tokens, parameters, and constraints gives you the power to build
clean, maintainable APIs.
In ASP.NET Core Web API, controller action methods return data to the client. To standardize this
process, ASP.NET Core provides ActionResult and various return types that represent HTTP
responses like 200 OK, 404 Not Found, 400 Bad Request, etc.
What is ActionResult?
• The HTTP status code and headers associated with the response
Syntax:
1. IActionResult
• This is a general return type for any action that returns an HTTP response.
fi
fi
fl
• It doesn't carry the data directly—only the response structure.
Example:
2. ActionResult<T>
• Lets you return either a strongly-typed result (like a Book object) or an error response.
Example:
kotlin
CopyEdit
return book; // Implicitly returns Ok(book)
}
Advantages:
• string
• JsonResult
• ObjectResult
• ContentResult
Method Description
Ok() 200 OK response
NotFound() 404 Not Found
BadRequest() 400 Bad Request
NoContent() 204 No Content
Created() 201 Created
Unauthorized() 401 Unauthorized
Forbid() 403 Forbidden
Con ict() 409 Con ict
StatusCode(int Custom HTTP
code) status
Conclusion
Use ActionResult<T> when returning data and want exibility to also return errors.
What is a Model?
In ASP.NET Core MVC, a model is a representation of the data used in the application. Models
contain the business logic and validation rules for the application. They interact with the database
and form the structure for the data returned to the views.
◦ You can de ne properties that correspond to the data you wish to represent.
fl
fl
fi
fl
2. csharp
CopyEdit
10. Usage:
◦ The model can now be used within your controllers to transfer data between your
views and database.
In the MVC (Model-View-Controller) pattern, the Model acts as the data layer and is responsible
for the following:
• Business Logic: Handles any business rules associated with the data.
• Retrieving Data: Controllers use models to get data from databases or external sources,
perform operations, and pass the data to views.
csharp
CopyEdit
• {
• var products = _context.Products.ToList();
• return View(products); // Passing model to the view
• }
•
What is a ViewModel?
A ViewModel is a special type of model used speci cally for passing data from the controller to the
view. It is often a composite model, combining data from multiple sources to tailor the data needed
for a particular view.
• To structure the data in a way that makes sense for the view, without altering the actual
model.
Example of ViewModel:
csharp
CopyEdit
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public string Discount { get; set; }
}
fi