开发者

How to get the complete request that calls my MVC2 controller?

开发者 https://www.devze.com 2023-01-15 13:20 出处:网络
Newbie question … sorry ;-) I have to write and to integrate a new website in a complex web application.

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, "");
        }
      }
    }


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)
{

}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号