-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNote 5.txt
91 lines (48 loc) · 5.82 KB
/
Note 5.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
What is Routing?
Routing is functionality that map incoming request to the route handler. The route can have route parameters to receive values from the URL. Using the route, routing can find a route handler based on the URL. All the routes are registered when the application is started. There are two types of routing supported by ASP.NET Core
The conventional routing
Attribute routing
The Routing uses routes to map incoming requests with the route handler and Generates URL that is used in response. Mostly, the application has a single collection of routes and this collection is used for the process of the request. The RouteAsync method is used to map incoming requests (that match the URL) with available in route collection.
How Routing works in ASP.NET Core?
Routing is used to handle incoming HTTP requests for the app. Routing finds matching executable endpoint for incoming requests. These endpoints are registered when app starts. Matching process use values from incoming request url to process the requests. You can configure the routing in middleware pipeline of configure method in startup class.
app.UseRouting(); // It adds route matching to middleware pipeline
// It adds endpoints execution to middleware pipeline
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
When will you prefer attribute routing over conventional routing?
In ASP.NET Core, you can define routes for your web application using either attribute routing or conventional routing. The choice between the two depends on the complexity and requirements of your application. Here are some scenarios where you might prefer attribute routing over conventional routing:
Fine-grained control: Attribute routing allows you to define routes directly on the action methods or controllers using attributes. This gives you more granular control over the routing behavior for specific actions. You can have different routes for different actions, making it easier to manage and understand the routing logic.
Controller-specific routes: With attribute routing, you can define routes specific to a controller by placing the routing attributes directly on the controller class. This is useful when you have actions that share a common route prefix, simplifying the route definitions.
Action-specific routes: In some cases, you may want to have multiple routes for a single action method. Attribute routing allows you to define additional routes for an action by adding multiple attributes on the same method.
Complex routing requirements: If your application has complex routing requirements, attribute routing can provide a more straightforward and concise way to handle those scenarios. You can use route parameters, optional segments, and custom route constraints directly in the attribute, making the routing logic more readable.
On the other hand, conventional routing can be more suitable for simpler applications with straightforward routing requirements. It relies on the route template defined in the Startup class, which makes it easier to see all the routes at once. It's a good choice when your application has a small number of routes and doesn't require fine-grained control over individual actions.
What are the important route constraints?
In ASP.NET Core, route constraints are used to restrict the values that can be matched for route parameters. They are an essential part of defining more specific and controlled routes. Here are some important route constraints you can use:
int: Constrains the parameter to be an integer.
long: Constrains the parameter to be a long integer.
bool: Constrains the parameter to be a Boolean value, i.e., "true" or "false".
double: Constrains the parameter to be a double-precision floating-point number.
float: Constrains the parameter to be a floating-point number.
guid: Constrains the parameter to be a GUID (Globally Unique Identifier).
datetime: Constrains the parameter to be a valid date and time value.
alpha: Constrains the parameter to contain only letters (no digits or special characters).
regex: Allows you to define a custom constraint using a regular expression pattern.
length: Constrains the parameter to have a specific length. For example, {id:length(5)} will only match when the id parameter has a length of 5 characters.
min and max: Allows you to specify minimum and maximum values for numeric parameters. For example, {age:min(18)} will only match if the age parameter is 18 or greater.
range: Similar to min and max, but allows you to specify a range of values. For example, {year:range(1900, 2023)} will only match if the year parameter is between 1900 and 2023.
required: Indicates that the parameter is required and must be present in the URL for the route to match.
nonempty: Ensures that the parameter is not empty (not null, empty string, or whitespace).
maxlength and minlength: Restricts the length of a string parameter. For example, {username:maxlength(20)} will only match if the username parameter has a length of 20 characters or less.
What is the purpose of the wwwroot folder?
The wwwroot folder is a special folder in an ASP.NET Core web application that serves as the web root. Its purpose is to store static files, such as HTML, CSS, JavaScript, images, and other client-side assets that need to be directly accessible by the web browser.
When a web application receives a request, the web server looks for the requested resource within the wwwroot folder. If the resource is found in this folder, the web server serves it directly to the client without involving the ASP.NET Core middleware pipeline.
How do you change the path of wwwroot folder?
We need to set path of the wwwroot folder in the WebRootPath propertyof the WebApplicationOptions class.
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() {
WebRootPath = "foldername"
});