views:

47

answers:

2

Newbie question … sorry ;-)

I have to write and to integrate a new website in a complex web application.

My new (MVC2) website will be hosted on a separate server and only called when the user clicks on a link in the already existing, complex website.

Means I(!) define the URL which calls my(!) new website.

But “they” (the calling, already existing, complex web application/website) will add an attribute to the url. This attribute is the sessionID.

Ok, I think I understand already that this calls my (MVC2) controller.

But how can I get in my (MVC2) controller the “calling URL” (which include the added sessionID)?

Hopefully that someone understand what I ask ;-)

Thanks in advance!


I want just share my little parser - hopefully it helps someone. ;-)

Also requests like

(Request.Url.Query =) "?sessionID=12345678901234567890123456789012&argumentWithoutValue&x=1&y&z=3"

will be well parsed. Here my code:

Hashtable attributes = new Hashtable();
string query = Request.Url.Query;

string[] arrPairs = query.Split('&');       // ...?x=1&y=2
if (arrPairs != null)
{
  foreach(string s in arrPairs)
  {
     if (!String.IsNullOrEmpty(s))
     {
        string onePair = s.Replace("?", "").Replace("&", "");

        if (onePair.Contains("="))
        {
          string[] arr = onePair.Split('=');
          if (arr != null)
          {
            if (arr.Count() == 2)
            {
               attributes.Add(arr[0], arr[1]);
            }
          }
        }
        else
        {
          // onePair does not contain a pair!
          attributes.Add(onePair, "");
        }
      }
    }
+1  A: 

In your controller you still have direct access to the Request object, so you can use Request.Url, etc.

Does that answer your question, or is it something else that you need?

Paul Hadfield
thank you very much for your fast reply. "string query = Request.URL.Query;" thats what i searched basically ... but MVC has this nice "MapRoute" ... so i will try this first. Thanks!
+2  A: 

You really should set your URL and Route to be more MVC-Like. The URL you are calling should be:

newapp/controller/action/sessionId

Then set your route up:

routes.MapRoute(
    "sessionId",
    "{controller}/{action}/{sessionId}",
    new { controller = "controller", action = "action", sessionId = 0 });

Then in your controller:

public ActionResult Action(int sessionId)
{

}
Martin
Good point, if that's what @uwe123 wants to do it negates the need to query the Request object inside the controller (which does give you access to the raw URL, etc)
Paul Hadfield
Yes, thats it! Setting the MapRoute correct and define how my new website must be called! When I have further needs to parse the request I will use "string query = Request.URL.Query;" ... Perfect and thanks for the fast reply!