tags:

views:

179

answers:

2

Hello, I've started with ASP.NET MVC recently, reading blogs, tutorials, trying some routes, etc. Now, i've stumbled on a issue where i need some help.

Basically, i have an URL like /products.aspx?categoryid=foo&productid=bar

Most tutorials/examples propose to map this to something like: /products/category/foo/bar where "products" is the controller, "category" is the action, etc.

But i need to map it to /products/foo/bar. (without "category")

Is it possible? Am i missing something? Help will be highly appreciated. Thank you advance :)

P.S. Sorry for my bad English.

+4  A: 

(your English is just fine, no need to apologize!)

You can define a route like this:

routes.MapRoute("productsByCategory", "products/{category}/{productid}",
  new { controller="products", action="findByCategory" })

This will match

products/foo/bar and call an action looking like this:

public class ProductsController : Controller
{
   ...

   public ActionResult FindByCategory(string category, string productid)
   {
          ....
   }
}

does this help?

Ben Scheirman
Works like a charm! I'm starting like the framework more and more L:)
ciscocert
A: 

You also might consider making a Controller to test out your custom routes...

Check out Stephen Walther's blog entry about it.

Elijah Manor
You should write unit tests to test your custom routes. It's automated and we need to run it every time we make a change to controllers, actions, or routes, so why rely on a manual check?
Ben Scheirman