2

I build an url for specific controller with:

 var f = new System.UriBuilder( Request.Url.AbsoluteUri ) {
           Path = Url.Action( "Get", "People")
        };

it returns

http://localhost/myApp/People/Get

Good.

What is the best way to add a querystring to that url ?

Means

http://localhost/myApp/People/Get?id=7

I'm going to do with :

string path = string.Concat(f.ToString(), "?id=3");

But I want to know if is other way (more better than provided by me above) to append querystring to that Url.

Snake Eyes
  • 16,287
  • 34
  • 113
  • 221

2 Answers2

0
Url.Action( "Get", "People", new { id = 7 })
Samich
  • 29,157
  • 6
  • 68
  • 77
  • It's because you have route for `id` param. It's actually should be `http://localhost/myApp/People/Get/7` which is redable. If you want to add exactly `?id=3` - probably concatenation is best way, or change default route to not use `id` param (which is not recomended) – Samich Aug 14 '12 at 06:48
  • I understand, that why I asked for better way to do without concatenation, I'm avoiding hardcoding. – Snake Eyes Aug 14 '12 at 06:51
  • Again, if you don't have route for `personValue` param it will generate `/People/Get?personValue=7` (just tried it) – Samich Aug 14 '12 at 06:59
0

Maybe try one of the extension methods shown here: How to build a query string for a URL in C#?

It will lead to more code sure but more flexibility i guess.

Community
  • 1
  • 1
Oleg
  • 422
  • 3
  • 13