This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16820|Reply: 0

[ASP.NET] The MVC view gets the current controller and method

[Copy link]
Posted on 4/21/2016 6:55:23 PM | | | |


Not to mention nonsense, first of all, despise Baidu!! I can't search for anything, search for the same keyword, Google is the first answer, from Baidu search, it's all nonsense!!

sb Baidu !!


In the controller:

In the controller class Controller in ASP.NET MVC, there is this read-only property:




Contains route information. The RouteData class belongs to the System.Web.Routing namespace. This indicates that this class is not a class exclusive to ASP.NET MVC, but belongs to the ASP.NET.

ASP.NET MVC uses this route to start the controller and the corresponding action.

Take a closer look at this RouteData type:



There is a Values property of type RouteValueDictionary that reads out the values of the route. We can use this property to get the value we want.

Write action code:


in View
If in View, we can also get the ControllerContext completely through the value of the ViewContext property of the WebViewPage. The reason is that the ViewContext inherits from the ControllerContext.



The ControllerContext class has the RouteData property mentioned at the beginning of this article



In this way, we can display the names of the current controllers and actions in the view with the following code:







Previous:Regarding the issue with ajax.beginform not executing the success method in MVC
Next:Winform obtains all the IP addresses of the machine and filters the IP addresses of the private network
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com