Get Action, Controller, Area Name in MVC ASP.NET C# 100% successful

Thảo luận trong 'Lập Trình Website MVC5 & MVC6' bắt đầu bởi admin, 24/9/18.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,905
    Đã được thích:
    1,199
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Việc lấy Controller Name và Action Name, Area Name để xử lý dữ liệu rất quan trọng khi lập trình web asp.net mvc c# mà không bạn nào tránh khỏi. Hôm nay itseovn sẽ hướng dẫn các bạn bài : Get Action and Controller Name Area in MVC ASP.NET C# 100% successful (lấy Action và Controller và Area Name toàn tập)

    get-action-name-controller-name-area-name-in-mvc.jpg

    Các bạn linh hoạt sử dụng các cách sau đây để lấy Action Name, Controller Name, Area Name tùy vào bạn sử dụng lấy ở vị trí nào nhé.

    Cách 1: Lấy Controller Name và Action Name trong Class/Global.asax như sau:
    Mã:
    HttpContextBase context = new HttpContextWrapper(HttpContext.Current);
    RouteData routedt = RouteTable.Routes.GetRouteData(context);
    if (routedt != null)
    {
        var controllername = routedt.GetRequiredString("controller");
        var actionname =  routedt.GetRequiredString("action");
    }
    
    Cách 2: Lấy Controller Name và Action Name ở vị trí bất kỳ như View,..
    Mã:
    // Tác chuỗi URL nếu có QueryString
    var fullUrl = Request.UrlReferrer.ToString();
    var questionMarkIndex = fullUrl.IndexOf('?');
    string queryString = null;
    string url = fullUrl;
    if (questionMarkIndex != -1) // nếu có QueryString
    { 
        url = fullUrl.Substring(0, questionMarkIndex);
        queryString = fullUrl.Substring(questionMarkIndex + 1);
    }
    
    // Cho dữ liệu vào mảng
    var request = new HttpRequest(null, url, queryString);
    var response = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(request, response);
    var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
    
    // Lấy dữ liệu ra ngoài
    var values = routeData.Values;
    var controllerName = values["controller"];
    var actionName = values["action"];
    var areaName = values["area"];
    
    Cách 3: Lấy Controller Name và Action Name bằng url web mvc
    Mã:
    var url = Request.UrlReferrer.GetLeftPart(UriPartial.Path);
    var querystring = Request.UrlReferrer.Query.Length > 0 ? uri.Query.Substring(1) : string.Empty;
    
    // Arranges
    var request = new HttpRequest(null, url, queryString);
    var response = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(request, response)
    
    var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
    
    // Extract the data 
    var values = routeData.Values;
    var controllerName = values["controller"];
    var actionName = values["action"];
    var areaName = values["area"];
    
    Cách 4:
    Mã:
    var values = RouteDataContext.RouteValuesFromUri(Request.UrlReferrer);
    var controllerName = values["controller"];
    var actionName = values["action"];
    
    Cách 5: Lấy Action và Controll Name thông qua Controller khác
    Mã:
    string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
    string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();
    
    Hoặc
    Mã:
    var actionName = RouteData.Values["controller"];
    var controllerName = RouteData.Values["action"];
    
     
    Cảm ơn đã xem bài:

    Get Action, Controller, Area Name in MVC ASP.NET C# 100% successful

    Chỉnh sửa cuối: 24/9/18
    seolagi thích bài này.


Chủ để tương tự : Action Controller
Diễn đàn Tiêu đề Date
Lập Trình Website MVC5 & MVC6 Lấy Action và Controller trong Global.asax trong MVC C# ASP.NET như thế nào? 24/9/18
Lập Trình Website MVC5 & MVC6 Set multipart/form-data file upload bằng js data-action form như thế nào? 19/10/18
Lập Trình Website MVC5 & MVC6 RedirectToAction with parameter trong MVC C# ASP.NET 2/9/18
Lập Trình Website MVC5 & MVC6 Lấy giá trị Html.CheckBox bằng Request.Form trong Controller MVC C# ASP.NET 7/8/18
Lập Trình Website MVC5 & MVC6 AngularJs $http.post() không gửi data về Controllers trong MVC 29/10/17