WebApiWith.NET
Samples and resources of how to design WebApi with .NET
Install / Use
/learn @oskardudycz/WebApiWith.NETREADME
WebApi with .NET
Samples and resources of how to design WebApi with .NET
- WebApi with .NET Core
Support
Feel free to create an issue if you have any questions or request for more explanation or samples. I also take Pull Requests!
💖 If this repository helped you - I'd be more than happy if you join the group of my official supporters at:
Prerequisites
- Install .NET Core SDK 3.1 from link.
- Install one of IDEs:
- Visual Studio - link - for Windows only. Community edition is available for free,
- Visual Studio for Mac - link - for MacOS only. Available for free,
- Visual Studio Code- link with C# plugin - Cross-platform support. Available for free,
- Rider - link - cross-platform support. Paid, but there are available free options (for OpenSource, students, user groups etc.)
Project Configuration
Routing
From the documentation: "Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints."
Saying differently routing is responsible for finding exact endpoint based on the request parameters - usually based on the URL pattern matching.
Endpoint executes the logic that creates an HTTP response based on request.
To use routing and endpoints it's needed to call UseRouting and UseEndpoints extension method on app builder in Startup.Configure method. That will register routing in middleware pipeline.
Note that those methods should be registered in the order as presented above. If the order is changed then it won't be registered properly.
Route templates
Templates add flexibility to supported URL definition.
The simplest option is static URL where you have just URL, eg:
/Reservations/List/GetUsers/Orders/ByStatuses/Closed
Route parameters
Static URLs are fine for the list endpoints, but if we'd like to get a list of records.
To allow dynamic matching (eg. reservation by Id) we need to use parameters. They can be added using {parameterName} syntax. eg.
/Reservations/{id}/users/{id}/orders/{orderId}
They don't need to be only used instead of concrete URL part. You can also do eg.:
/Reservations?status={reservationStatus}&user={userId}- this will get parameters from the query string and match eg./Reservations?status=Open&userId=123and will havestatusparameter equal toOpenanduserIdequal to123,/Download/{fileName}.{extension}- this will match eg./Download/testFile.txtand end up with two route data parameters -fileNamewithtestFilevalue andextensionwithtxtaccordingly,/Configuration/{entityType}Dictionary- this will match/Configuration/OrderStatusDictionaryand will haveentityTypeparameter withOrderStatusvalue.
You can also add catch-all parameters - {**parameterName}, that can be used as fallback when no route was found:
/Reservations/{id}/{**reservationPath}- this will match eg./Reservations/123/changeStatus/confirmedand will havereservationPathparameter withchangeStatus/confirmedvalue
It's also possible to make the parameter optional by adding ? after its name:
/Reservations/{id?}- this will match both/Reservationsand/Reservation/123routes
Route constraints
Route template parameters can contain constraints to narrow down the matched results. To use it you need to add constraint name after parameter name {prameter:constraintName}.
There is a number of predefined route constraints, eg:
/Reservations/{id:guid}- will match eg./Reservations/632863d2-5cbf-4c9f-92e1-749d264d965ebut wont' match eg./Reservations/123,/Reservations/top/{limit:int:minlength(1):maxLength(10)- this will allow to pass integers between1and10forlimitparameter. So it will allow to get at most top 10 reservations,/Inbox?from={fromEmailAddress:regex(\\[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})}- regex can be also used to eg. check email address or provide more advanced format check. This will match/Inbox?from=john.doe@company.comand will havefromEmailAddressparameter withjohn.doe@company.comvalue,- see more constraints examples in route constraint documentation.
Note - failing constraint will result with 400 - BadRequest status code, however, the messages are generic and not user friendly. So if you'd like to make them more related to your business case - it's suggested to do move it to validation inside the code.
You can also define your custom constraint. The sample use case would be when you want to provide the validation for your business id format.
See sample that validates if reservation id is built from 3 non-empty parts split by |;
public class ReservationIdConstraint : IRouteConstraint
{
public bool Match(
HttpContext httpContext,
IRouter route,
string routeKey,
RouteValueDictionary values,
RouteDirection routeDirection)
{
if (routeKey == null)
{
throw new ArgumentNullException(nameof(routeKey));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
if (!values.TryGetValue(routeKey, out var value) && value != null)
{
Related Skills
diffs
344.1kUse the diffs tool to produce real, shareable diffs (viewer URL, file artifact, or both) instead of manual edit summaries.
clearshot
Structured screenshot analysis for UI implementation and critique. Analyzes every UI screenshot with a 5×5 spatial grid, full element inventory, and design system extraction — facts and taste together, every time. Escalates to full implementation blueprint when building. Trigger on any digital interface image file (png, jpg, gif, webp — websites, apps, dashboards, mockups, wireframes) or commands like 'analyse this screenshot,' 'rebuild this,' 'match this design,' 'clone this.' Skip for non-UI images (photos, memes, charts) unless the user explicitly wants to build a UI from them. Does NOT trigger on HTML source code, CSS, SVGs, or any code pasted as text.
openpencil
2.0kThe world's first open-source AI-native vector design tool and the first to feature concurrent Agent Teams. Design-as-Code. Turn prompts into UI directly on the live canvas. A modern alternative to Pencil.
HappyColorBlend
HappyColorBlendVibe Project Guidelines Project Overview HappyColorBlendVibe is a Figma plugin for color palette generation with advanced tint/shade blending capabilities. It allows designers to
