views:

2863

answers:

5

How do I get the complete request URL (including query string) in my controller? Is it a matter of concatenating my URL and form parameters or is there a better way.

I checked this question, but it seems not to be applicable to MVC.

Correct me if I'm wrong.

--Edit (extended problem description)--
My call to my SearchController.AdvancedSearch() originates from a form with about 15 optional parameters. Because of the number of parameters and no possible way (I think) to have optional parameters passed in a clean way, I went for a catchall string that is handled in the controller.
Now I want my controller to store its call in a breadcrumb component so that when the crumb is clicked the exact same result can be fetched (with all, but not more, of the arguments included). To do this I need the entire request.URL, including the querystring. In Request.RawURL, request.URL etc this query string is not included. In fact currently I do a plain String.Format("{0}/{1}", request.Url, request.form). This gives me some weird results (like submit button values etc), but it works. If there are suggestions on how to make this less of an ugly hack, please do tell.

A: 

You can get at the current Request object by using:

HttpContext.Current.Request

However, I've gotta ask -- why do you want the request URL? By doing this, you've made your controller dependent on the Request object, which will make your unit tests much harder to write.

Roger Lipscombe
A: 

Thanks for the reply. I know where to find the current request, but I can't seem to find a raw url anywhere though. They are all deprived from the querystring and I need that bit too.

My controller updates a collection of urls in my BreadCrumb component. Therefor it needs the request URL. How do you suggest I take on this problem?

borisCallens
+1  A: 

HttpContext.Current.Request.RawUrl
HttpContext.Current.Request.QueryString

As far as your second question. Instead of tracking the urls, querystring info, etc, that your user has gone to. Track the controllers and actions called.
That would remove the need for the Request object entirely.

Jeff Sheldon
Thanks for the answer. I will updated my question to make my situation a bit more clear. Please have a look.
borisCallens
A: 

Within the controller, you can access Request.RawUrl.

Haacked
Thanks for the answer, but request.rawurl doesn't contain the querystring
borisCallens
A: 

I just tried it in an ASP.NET MVC application I have here. I added a query string in Internet Explorer (i.e. "http://localhost:53432/Home?foo=bar"). I set a breakpoint in HomeController.Index. Looking at Request in the QuickWatch window, I can see my query string in both Url and RawUrl.

Is this not working for you?

Roger Lipscombe
No. But if it's working for you, it's probably something I'm doing wrong then :(
borisCallens
Do you have any custom routes set up? I only have the defaults. Maybe you're accidentally discarding stuff there?
Roger Lipscombe