Ways to get the name of a controller in ASP.

Source: Internet
Author: User
1, http://www.php.cn/code/6617.html "target=" _blank "> View

String controller = ViewContext.RouteData.Route.GetRouteData (this. Context). values["Controller"].    ToString (); String controller = viewcontext.routedata.values["Controller"]. ToString ();

2. In the action of the Controller

String controller = RouteData.Route.GetRouteData (this. HttpContext). values["Controller"].    ToString (); String controller = routedata.values["Controller"]. ToString ();

3, the filter

For example, in ActionFilterAttribute, this is usually the time to implement an inheritance class, and then rewrite the related methods.

In the overridden method, if the name of the controller is required.

<summary>///Authentication permission to check if the user is logged in (the action will be performed before execution)//</summary>///<param name= "Filtercontext" > </param>public override void OnActionExecuting (ActionExecutingContext filtercontext) {base.       OnActionExecuting (Filtercontext); String controller = filtercontext.routedata.values["Controller"]. ToString (); controller = controller + "Controller";}

4. In Public methods

<summary>///gets the controller full name of the current page///</summary>///<returns></returns>public string Getcurrentcontroller () {String controller = httpcontext.current.request.requestcontext.routedata.values[" Controller "]. ToString (); if (!string. Isnullorwhitespace (Controller) {controller = controller + "Controller";} Else{controller = "";} return controller;}

The above is the content of the method of obtaining the controller name in ASP. Topic.alibabacloud.com (www.php.cn) for more information!

  • 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.