I came into contact with the asp.net mvc framework two days ago. Thanks to the excellent tutorials in the blog.
During this period of time, the teacher asked me to build a project and thought about how to use the asp.net mvc Framework. He forced himself to learn more, so he put on his head! One such problem exists. The pages under the sysmanage file under my views file only implement a single function .. in this case, you want to assume a permission. the user logs on and obtains the user information! Obtain the permissions of the user and compare whether the user has the permission to access the page!
First, I want to use an interceptor! However, I don't know how to get the action method name. Because the action method name corresponds to the name of the page to be accessed, in this way, we can compare it with the database to determine whether the user has the permission to access the page! I knocked on the code myself. I checked some attribute methods and found out how to get the action method name.
Example:
Reference content is as follows: Public class VisitFilter: FilterAttribute, IActionFilter { /// <Summary> /// Intercept the action before calling it /// </Summary> /// <Param name = "myFilterContext"> </param> Void IActionFilter. OnActionExecuting (ActionExecutingContext myFilterContext) { // Assume that the user's permissions are obtained. String UserPower = "UserPower "; If (UserPower! = MyFilterContext. ActionDescriptor. ActionName) { Throw new InvalidOperationException (@ "no access permission "); } } Void IActionFilter. OnActionExecuted (ActionExecutedContext myFilterContext) { } } |
But the next question is, how can I go to its custom page without permission? Can I transfer an action to another action ?...?
With this question, I kept looking at methods, attributes, and I couldn't find out how to do it. but it seems impossible to think about it. (if any of you know the method, please share it with me. I sincerely thank you ).
The above code throws an exception .. yes, it's just him, because we can also write an exception interceptor. in this way, we can capture the exceptions we throw, and define the error information by ourselves .. when an exception occurs, the system automatically jumps to Views/Shared/Error. aspx. we will display our own error messages on the page to prompt users .. this will achieve the effect .. haha. the exception interceptor code is as follows:
Reference content is as follows: Public class ExceptionFilter: FilterAttribute, IExceptionFilter { /// <Summary> /// Intercept an exception in action /// </Summary> /// <Param name = "myFilterContext"> </param> Void IExceptionFilter. OnException (ExceptionContext myFilterContext) { MyFilterContext. Controller. ViewData ["ErrorMsg"] = myFilterContext. Exception. Message; MyFilterContext. Result = new ViewResult () { ViewName = "Error ", ViewData = myFilterContext. Controller. ViewData, }; MyFilterContext. ExceptionHandled = true; } } |
Here I modified it directly on the page that comes with the asp.net mvc Framework, and verified the download of the About source code...
Author blog: http://www.cnblogs.com/NetChessman/