locked
How to get the controller and action names or even the route name from an absolutepath? RRS feed

  • Question

  • User-319159958 posted

    I have an action method which when it is called it needs to push a new cookie in the response and then redirect to almost the same url it was but changing one action parameter value.

    So I get the url to redirect to from the request.UrlRefferer.

    But then I would like to extract the exact controller name and action name to then redirect using routing.

    Better yet would be if I could match (or extract) this UrlReferrer value to an actual routename declared in my global.asax

    I omit the code where I push the cookie to the response because thats I got figured out.

    Dim returnUrl As String = ""
    
    If Not Request.UrlReferrer Is Nothing Then
    
                    returnUrl = Request.UrlReferrer.AbsolutePath
    
    End If
    

    How can I get the route values from this?

    Also what if my Url is this:

    FeastingPlaces/2/toronto

    and my declared route is this one:

     routes.MapRoute( _
                                      "RestaurantspageRoute", _
                                      "FeastingPlaces/{cid}/{URLCity}", _
                                      New With {.controller = "FeastingPlaces", .action = "Index", .cid = UrlParameter.Optional, .URLCity = UrlParameter.Optional}, New String() {"SocialMVC.SocialMVC"} _
                                  )
    
    
    


    Where the actual action is not in the actual url

    Monday, August 13, 2012 3:03 PM

Answers

  • User-1828494216 posted

    I'm afraid I don't understand you completely. I'll try to answer your question.

    You can set cookie in your Action like this:

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";
    
        Response.Cookies.Add(new HttpCookie("cookieName", "cookieValue"));
    
        return View();
    }

    You can get Controller and Action name like this:

    string controllerName = ControllerContext.RouteData.Values["Controller"].ToString();
    string actionName = ControllerContext.RouteData.Values["Action"].ToString();

    You can get queryString values like this

    string querryStringVal = Request.QueryString["MyKey"].ToString();

    You can also loop trough...

    string querrystring = string.Empty;
    foreach (KeyValuePair<string,string> item in Request.QueryString)
    {
        querrystring += string.Concat(item.Key, "=", item.Value, "&");
    }
    if (querrystring.Length > 0)
        querrystring.TrimEnd('&');

    There are many methods to redirect a user from your Action. RedirectRedirectToActionRedirectToRoute.

    If you want to parse url then you can use regular expressions.

    There could be some typos because I haven't tested this. The concept should work.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 13, 2012 5:33 PM

All replies

  • User-1828494216 posted

    I'm afraid I don't understand you completely. I'll try to answer your question.

    You can set cookie in your Action like this:

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";
    
        Response.Cookies.Add(new HttpCookie("cookieName", "cookieValue"));
    
        return View();
    }

    You can get Controller and Action name like this:

    string controllerName = ControllerContext.RouteData.Values["Controller"].ToString();
    string actionName = ControllerContext.RouteData.Values["Action"].ToString();

    You can get queryString values like this

    string querryStringVal = Request.QueryString["MyKey"].ToString();

    You can also loop trough...

    string querrystring = string.Empty;
    foreach (KeyValuePair<string,string> item in Request.QueryString)
    {
        querrystring += string.Concat(item.Key, "=", item.Value, "&");
    }
    if (querrystring.Length > 0)
        querrystring.TrimEnd('&');

    There are many methods to redirect a user from your Action. RedirectRedirectToActionRedirectToRoute.

    If you want to parse url then you can use regular expressions.

    There could be some typos because I haven't tested this. The concept should work.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 13, 2012 5:33 PM
  • User146802239 posted

    you need to write actionmathod parameter is like this

    Public Action FeastingPlaces(int? cid,string URLCity)

    {

    return view();

    }

    Tuesday, August 14, 2012 1:24 AM
  • User-1652363262 posted

    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    is this what you need? 

    Tuesday, August 14, 2012 4:20 AM