2

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.

2 Answers 2

5

(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?

Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm! I'm starting like the framework more and more L:)
0

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

Check out Stephen Walther's blog entry about it.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.