How to get all controller attribute action and parameter in Asp.net MVC

Source: Internet
Author: User

We are considering using MVC to revise our website. Although we have been paying attention to it since the earliest launch of Asp.net MVC, we have never done any related projects or even some decent exercises. This part of permission has been read in many articles on the Internet, some are implemented by inheriting authorize, and some are implemented by using actionfilter. As an article on the Internet suggests, authorize is originally used for verification, authentication and authorization should be separated, so I also want to put the permission control in the actionfilter for processing. This requires that the controller, action, and Param of the current request be known, and the current user must need it, you only need to use filtercontext in onactionexecuting (actionexecutingcontext filtercontext) to obtain the following information:

 

String controller = filtercontext. actiondescriptor. controllerdescriptor. controllername;
String action = filtercontext. actiondescriptor. actionname;
String username = filtercontext. httpcontext. User. Identity. Name;
Routevaluedictionary parameters = filtercontext. routedata. values;

You can obtain the specific parameters of the request and then process parameters.

Next, you can determine whether the current user has the permission to operate the action based on the permission information configured in the database.

So how can we configure all the Controller action parameter and other information in the entire project? We won't try to find them one by one, therefore, you can obtain all the Controller action parameter information in the project through reflection on the permission configuration page. The reference code is as follows:

VaR allcontrollers = assembly. getcallingassembly (). gettypes (). Where (type => type. issubclassof (typeof (Controller). tolist ();
Foreach (VAR controller in allcontrollers)
{
Foreach (VaR method in controller. getmethods (bindingflags. Public | bindingflags. instance ))
{
If (method. returntype = typeof (actionresult ))
{
// Obtain the attributes of these methods. You need to obtain the get or post attributes. the time zone for permission is divided into read/write operations.
Foreach (VAR attribute in method. getcustomattributes (typeof (system. Web. MVC. httppostattrites), true ))
{
Httpcontext. Current. response. Write ("[httppost] <br> ");
}

// Obtain the name of the controller and the name of the method whose return type is actionresult.
Httpcontext. Current. response. Write (controller. Name + "." + method. Name + "(");

// Obtain all parameters of the method and sort the parameters by location
VaR parameters = method. getparameters (). orderby (P => P. position );
Foreach (VaR parameter in parameters)
{
// Parameter type and name in the output method
Httpcontext. Current. response. Write (parameter. parametertype + "" + parameter. Name );

// If the parameter has a default value, the default value of the output parameter
If (! String. isnullorwhitespace (parameter. defaultvalue. tostring ()))
{
Httpcontext. Current. response. Write ("=" + parameter. defaultvalue );
}
// Determine whether it is the last parameter.
If (parameter. Position <parameters. Count ()-1)
{
Httpcontext. Current. response. Write (",");
}
}
Httpcontext. Current. response. Write (") <br> ");
}
}
}

 

The results in the above Code are output to the web page. You can remove these outputs and set some variables to save the information. The output format here is as follows:

 

Accountcontroller. login (system. String returnurl)

[Httppost]
Accountcontroller. login (mvcmaxrayweb. Models. loginmodel model, system. String returnurl)

Accountcontroller. logoff ()

Accountcontroller. Register ()

[Httppost]
Accountcontroller. Register (mvcmaxrayweb. Models. registermodel Model)

Accountcontroller. changepassword ()

[Httppost]
Accountcontroller. changepassword (mvcmaxrayweb. Models. changepasswordmodel Model)

Accountcontroller. changepasswordsuccess ()

Homecontroller. Index (system. int32 id = 1, system. int32 pagesize = 10)

Homecontroller. About ()

Homecontroller. Contact ()

Nopermissioncontroller. Index ()

 

 

The attribute is obtained to perform read/write operations in the time zone of the permission. The website will be published as appropriate after the permission is obtained.

 

Please respect my tossing results. If you need to reprint it, please keep the following information

Original address: http://www.cnblogs.com/kandy/archive/2012/06/15/2550424.html

Author: Chen Ping

Email: chenping@maxray.cn

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.