How do I get the Controller and Action names from the Referrer Uri?

前端 未结 9 920
清歌不尽
清歌不尽 2020-12-01 14:06

There\'s a lot of information for building Uris from Controller and Action names, but how can I do this the other way around?

Basically, all I\'m trying to achieve i

相关标签:
9条回答
  • 2020-12-01 14:21

    @gordon's solution works, but you need to use

     return RedirectToAction(actionName.ToString(), controllerName.ToString(),values);
    

    if you want to go to previous action

    0 讨论(0)
  • 2020-12-01 14:29

    This is a method I made to extract url simplified from referrer because I had token (finished with "))/") in my URL so you can extract easily controller and action from this:

    private static string GetURLSimplified(string url)
        {
            string separator = "))/";
            string callerURL = "";
    
            if (url.Length > 3)
            {
                int index = url.IndexOf(separator);
                callerURL = url.Substring(index + separator.Length);
            }
            return callerURL;
        }
    
    0 讨论(0)
  • 2020-12-01 14:32

    Here is a lightweight way to do this without creating response objects.

    var values = RouteDataContext.RouteValuesFromUri(Request.UrlReferrer);
    
    var controllerName = values["controller"];
    var actionName = values["action"];
    

    Uses this custom HttpContextBase class

    public class RouteDataContext : HttpContextBase {
        public override HttpRequestBase Request { get; }
    
        private RouteDataContext(Uri uri) {
            var url = uri.GetLeftPart(UriPartial.Path);
            var qs = uri.GetComponents(UriComponents.Query,UriFormat.UriEscaped);
    
            Request = new HttpRequestWrapper(new HttpRequest(null,url,qs));
        }
    
        public static RouteValueDictionary RouteValuesFromUri(Uri uri) {
            return RouteTable.Routes.GetRouteData(new RouteDataContext(uri)).Values;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:34

    To expand on gdoron's answer, the Uri class has methods for grabbing the left and right parts of the URL without having to do string parsing:

    url = Request.UrlReferrer.GetLeftPart(UriPartial.Path);
    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"];
    
    0 讨论(0)
  • 2020-12-01 14:35

    The RouteData object can access this info:

     var controller = RouteData.Values["controller"].ToString();
     var action = RouteData.Values["action"].ToString();
    
    0 讨论(0)
  • 2020-12-01 14:36

    I don't believe there is any built-in way to retrieve the previous Controller/Action method call. What you could always do is wrap the controllers and action methods so that they are recorded in a persistent data store, and then when you require the last Controller/Action method, just retrieve it from the database (or whatever you so choose).

    0 讨论(0)
提交回复
热议问题