2

I have a url that looks like this

http://mysite/account/login?returnurl=/Event/Details/41

and returnUrl = "/Event/Details/41"

I need to get the route values from return url - which includes the EventID = 41

I was able to get the event Id using this code:

public ActionResult Login()
{
   ....
   string returnUrl = Request.QueryString["ReturnUrl"];
   int lastIndex = returnUrl.LastIndexOf("/") + 1;
   string strEventID = returnUrl.Substring(lastIndex);
   int EventID = Int32.Parse(strEventID);
   ....
}

But I feel there could be a more flexible way that would give me access to querystrings, route values, etc, without manually doing it this way.

I dont't want to use WebRequest, WebClient and blah blah, just looking for MVC related solution or something simpler.

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • 2
    this should help - http://stackoverflow.com/questions/1481565/string-url-to-routevaluedictionary – Dmitry Khryukin Apr 09 '13 at 12:34
  • The solution on the link given by @DmitryKhryukin is the one to go for, and it still works for MVC3. In addition, the `action` is always the last key preceded by the `controller` key. So if you have parameters in your method they come before those two keys. (e.g. id, arg2, arg3, controller, action) – von v. Apr 09 '13 at 13:36
  • I think the link above says much about what I want to do. Most answers here were just repeating what I had in the question but using different approach. What I wanted was Parse the returnUrl and extract the route values e.g. `Id=4`, `Action=Details` and `Controller=Event`. And if there is a querystring in that returnUrl, I should be able to get it too – codingbiz Apr 09 '13 at 14:41

3 Answers3

2

In order to access to querystrings, you can directly put these in the action signature :

public ActionResult Login(string returnurl)
{
   ....
   if(!string.IsNullOrWhiteSpace(returnurl)) {
       int lastIndex = returnurl.LastIndexOf("/") + 1;
       string strEventID = returnUrl.Substring(lastIndex);
       int EventID = Int32.Parse(strEventID);
   }
   ....
}

EDIT :

In order to extract the route parameters from the returnurl, you can parse it via a regex :

Regex regex = new Regex("^/(?<Controller>[^/]*)(/(?<Action>[^/]*)(/(?<id>[^?]*)(\?(?<QueryString>.*))?)?)?$");
Match match = regex.Match(returnurl);

// match.Groups["Controller"].Value is the controller, 
// match.Groups["Action"].Value is the action,
// match.Groups["id"].Value is the id
// match.Groups["QueryString"].Value are the other parameters
Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
  • 1
    @codingbiz: I have added a sample code in order to parse the returnurl as you mentioned in your comment... – Samuel Caillerie Apr 09 '13 at 15:25
  • 1
    @codingbiz this is the right and good answer, what you mentioned in your question was you dont't wanted to use WebRequest, WebClient and by this solution you don't need to use them. and in the edit there are good regexs to get all value you need. – Kambiz Shahim Apr 09 '13 at 17:51
  • @SamuelCaillerie great job – Kambiz Shahim Apr 09 '13 at 17:52
1

try this:

public ActionResult Login(string returnUrl)
{
   ....
   var id = this.GetIdInReturnUrl(returnUrl);
   if (id != null) 
   {
   }
   ....
}

private int? GetIdInReturnUrl(string returnUrl) 
{
   if (!string.IsNullOrWhiteSpace(returnUrl)) 
   {
       var returnUrlPart = returnUrl.Split('/');
       if (returnUrl.Length > 1) 
       {
           var value = returnUrl[returnUrl.Length - 1];
           int numberId;
           if (Int32.TryParse(value, out numberId))
           {
               return numberId; 
           }
       }
   }

   return (int?)null;
}
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
0

Change your controller action to this one, during model binding stage ASP.NET MVC looks into the request and find the value of returnUrl and you don't need to handle it manually.

    public ActionResult Login(string returnUrl)
    {
        int EventID = 0;
        int.TryParse(Regex.Match(returnUrl, "[^/]+$").ToString(), out EventID);
        ....
    }
Kambiz Shahim
  • 2,560
  • 14
  • 21