can you explain me how to pass query string parameter in url vs. passing parameter as example /param1/2

nicedone
nicedone used Ask the Experts™
on
hi,

i am trying to learn if i want to pass a parameter as param1/3   vs replies?param1=3

i would like to learn how to generate each of them what should i do, and which 1 i should prefer in my coding?

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Alexandre SimõesManager / Solutions Architect

Commented:
The first one is the classic querystring way supported natively by ASP.net:
replies?param1=3

The second is based on routes, which is a commonly used in the MVC designs.
You can still use routes in ASP.net webforms, but ASP.net MVC makes it easier to implement.

So to put it simple, the choice is much related to how you want to build your site, Web Forms or MVC? :)

Author

Commented:
thanks for the explaination,

1) if i use mvc which i do at the moment and prefer to pass in

replies?param1=3 can i do that? if so could you show a little code snippet that accomplishes that

if i want to use param1/3 as in routes could you share a small code to accomlish that insted too
Manager / Solutions Architect
Commented:
The MVC routes don't work exactly like that.
The parameter name is not there, they are picked from where they are in the route.
For ex:
public class ReviewsController : Controller
{
    // eg: /reviews
    [Route("reviews")]
    public ActionResult Index() { ... }
    // eg: /reviews/5
    [Route("reviews/{reviewId}")]
    public ActionResult Show(int reviewId) { ... }
    // eg: /reviews/5/edit
    [Route("reviews/{reviewId}/edit")]
    public ActionResult Edit(int reviewId) { ... }
}

Open in new window

This means that the controller automatically maps the placeholder name with the method argument.

You can actually mix both routes and querystring in the same request:
    [Route("reviews/{reviewId}/edit")]
    public ActionResult Edit(int reviewId, String name) { ... }

Open in new window

Can be called: reviews/123/edit?name=Alex

The example above was taken from here:
http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

if i use mvc which i do at the moment and prefer to pass in replies?param1=3
Why would you want to do that anyway?
As I told you above, it's possible but you have to have a very specific reason for that...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial