views:

415

answers:

5

I have a site that external sites are linking to with a campaign ID

http://www.example.com?cid=123

or (with a slash)

http://www.example.com/?cid=123

In the case where you miss off the slash most browsers will go and add it in for you. So in either case, whichever you enter the URL that my application is hit is the second URL above. The QueryString parameter is always available.

I was noticing with ASP.NET MVC routing (maybe even without) that when I'm using a virtual directory I don't get this same behavior.

For instance if I have 'http://www.example.com/virtualdirectory?cid=123', this will reach my 'default.aspx' page (since it doesn't match any route). BUT when I examine the value of Request.QueryString it is blank. If I go to 'http://www.example.com/virtualdirectory?cid=123', then the QueryString value is present in the Request object.

If I put a breakpoint in Default.aspx.cs in a newly created MVC project (RTM version of MVC 1.0 - March 2009) then I won't see any query parameters.

i was wondering if there is any way of getting access to these parameters in IIS6 and/or IIS7 if the user accesses a virtual directory by means of a URL like /virtualdirectory?cid=123.

There may be no solution - but I'm jsut glad I spotted this before linking any partners into a virtual directory!

+1  A: 

One possible workaround --- I'm not sure I'd call it a solution --- would be to make a rewrite rule in IIS 7 or with a rewriter for IIS 6 to put the slash back in for you.

Craig Stuntz
A: 

What do you get when you try to access the raw URL using IIS server variables, specifically the HTTP_URL variable?

http://msdn.microsoft.com/en-us/library/ms524602.aspx

pbz
i'll check when i have a chance. Fortunately we havent given out many of these links yet. i just never knew about this behavior before
Simon_Weaver
+1  A: 

http://www.example.com?cid=123

this is easy with MVC. in your HomeController which is created for you, add a method attribute to your Index() method called String cid. like this:

        public ActionResult Index(string cid)
    {

Because it's a string it can be ignored, so that if you requested just the "http://www.example.com", the cid would be null. I've just tested this in one of my sites, and the value is passed in when you add the slash and without.

cottsak
A: 

you link should be of the form http://www.example.com/123

and since you are Action method "Index" is in the Home controller the default Index method will take care of this

public ActionResult Index(int id){}

to be specific you can use the AcceptVerbs attribute like this

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int id){}

The default route map should handle your request if you want to add more query string parameters like http://www.example.com/?cid=123&aid=456&bid=789 then add a separate route map to handle this URL and change you Action method accordingly

Rony
+1  A: 

Several of the answers do not understand the problem. If you put your application in a virtual directory, browsing to 'http://www.example.com/virtualdirectory?cid=123' vs 'http://www.example.com/virtualdirectory/?cid=123' has different behavior.

I just tried this using the Visual Studio Development Server and am able to reproduce the issue.

This does not appear to have anything to do with whether or not you have an argument on the Index method of the HomeController.

It appears to me that the web server is rewriting /virtualdirectory?cid=123 to /virtualdirectory/default.aspx? before asp.net gets its hands on the request.

JohnRudolfLewis
i think maybe theres nothing you can do - i was just surprised by the behavior and thought i'd ask. fortunately i found out how 'picky' it is sooner rather than later
Simon_Weaver