In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the controller and routing in asp.net core mvc". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the controller and routing are in asp.net core mvc.
1. Controller
First add a class under the Controllers directory called:
Public class DemoController
{
Public string Index ()
{
Return "Hello"
}
}
Access address:
Http://localhost:5006/demo/index
If nothing happens, you should be able to see the word "Hello" on the web page.
Create another class:
Using Microsoft.AspNetCore.Mvc
Public class NoContrl: Controller
{
Public IActionResult Index ()
{
Return Content ("Test")
}
}
Combining two unconventional controller classes, let's get a glimpse of how asp.net core MVC recognizes controllers. This is the best way to show that convention is better than configuration, as I said before. This philosophy was first proposed for MVC and was later extended to various aspects by. Net framework.
Asp.net core mvc identifies the controller, finds public classes that end with Controller or inherits from Controller in the project, and marks these classes as controllers. When receiving a request from the user or interface, the program parses the controller name from the request path and looks for the class of Controller or: Controller.
By default, an access URL is parsed into the following format in the program:
Other parameters of http://://[]
In the above, we know the resolution rules of the controller, so now take a look at the resolution rules of Action:
Add the following methods to DemoController:
Public int TestInt ()
{
Return 10
}
Public object TestObject ()
{
Return new
{
Name = "TestObject"
Age = 1
}
}
Public string TestPublic ()
{
Return "successfully visited TestPublic"
}
Protected string TestProtect ()
{
Return "successfully visited TestProtect"
}
Private string TestPrivate ()
{
Return "successfully visited TestPrivate"
}
Restart, and then visit the following address in turn:
Http://localhost:5006/Demo/TestInt
Http://localhost:5006/Demo/TestObject
Http://localhost:5006/Demo/TestPublic
Http://localhost:5006/Demo/TestProtect
Http://localhost:5006/Demo/TestPrivate
Then you can see that TestInt, TestObject, and TestPublic can all access normally, but both TestProtect and TestPrivate indicate that the web page cannot be found or cannot be accessed.
As you can see, to the program, Action is the exposed class method in the controller class, regardless of the return value of the method. That is, the program will find XXXController or a class named XXX that inherits Controller as the controller of XXX, then continue to find Action in this class, and return a request of 404 if it doesn't find it.
two。 Routin
In the first section, we introduced how asp.net core mvc finds the controller and Action, and this section shows how the program parses the name of the controller and Action, that is, the route map, from the request link.
Routing is responsible for matching incoming HTTP requests and then sending those requests to the application's executable endpoints. The endpoint is the executable request processing code unit of the application, that is, the method (Action) in our controller.
2.1 configuration of rout
Routes generated in the code are included for all asp.net core templates. Typically, we require that the route be configured in the Startup.Configure method.
Note that there is only one Configure method in the Startup class, and its overloaded version cannot appear.
The general declaration of this method is as follows:
Public void Configure (IApplicationBuilder app, IWebHostEnvironment env)
If you want to set up routing, you need to indicate that the project enables routing:
App.UseRouting ()
Then configure the route using the following methods:
App.UseEndpoints (endpoints = >
{
/ / configure routing
});
Usually for mvc projects, we generally configure routing in the following ways:
Endpoints.MapControllerRoute (
Name: "default"
Pattern: "{controller=Home} / {action=Index} / {id?}")
This line of code means: create a route for the mapping controller named default, and the mapping rule is {controller} / {action} / {id?}, that is, the first is the controller, the second is Action, and the third is ID, where ID may not exist. When Action cannot be resolved from the request address, the default is Index, and the controller defaults to Home.
Through this analysis, we can know that we have previously visited
Http://localhost:5000/
Which controller and what method to deal with-- HomeController.Index.
So let's modify the HomeController.Index to verify that we understand it correctly:
Public IActionResult Index ()
{
Return View ()
}
/ / modified to
Public IActionResult Index ()
{
Return Content ("Test")
}
Rerun the program to access
Http://localhost:5000/
Then the page appears: test, and you can see that the routing system automatically completes the controller name and action name for us. If a parameter appears in the method, the parameter is automatically viewed in the form of parameter name 1 = value 1 & parameter name 2 = value 2. Id is special and will automatically be mapped according to the directory. So:
Http://localhost:5000/ controller 1 / method 1 / id value
Http://localhost:5000/ controller 1 / method 1?id=id value
It's a request link.
2.2 add routing configuration
So, let's go back and look at the way to declare a route:
Public static ControllerActionEndpointConventionBuilder MapControllerRoute (this IEndpointRouteBuilder endpoints, string name, string pattern, object defaults = null, object constraints = null, object dataTokens = null)
By default, we do not set defaults, constraints, and dataTokens. These three parameters all have meaning. Instead of introducing the latter two, let's briefly introduce the first one:
In the routing configuration method, add the following:
Endpoints.MapControllerRoute (
Name: "test"
Pattern: "DemoTest/ {action=Index} / {id?}"
Defaults: new
{
Controller = "Demo"
});
So far, we have not created a controller named DemoTest, but we are accessing:
Http://localhost:5006/DemoTest
The response is still available, and the controller is parsed to Demo.
This is the meaning of defaults, when the route is parsed, the system will automatically populate the value in defaults into the value that is not set in the routing connection.
When we set up multiple routes, the routing system gives priority to trying to match the configuration that is easiest to parse. For example, when we visit:
Http://localhost:5000/DemoTest/
The routing system first parses from the configuration table called test, and only from other routes if it cannot be found here.
Thank you for reading, the above is the content of "what are controllers and routes in asp.net core mvc". After the study of this article, I believe you have a deeper understanding of what controllers and routes are in asp.net core mvc, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.