Help with MVC routing and multiple paramaters

Currently I have the following route that works fine, but is not great for SEO purposes

routes.MapRoute("Cars", "cars/detail/{id}/", 
                new { controller = "Cars", action = "Detail", id = UrlParameter.Optional},
                //new { id = @".*?(\\d+)" }, 
                new[]{"BarMotors.Controllers"}
                );

Open in new window


ViewModel
public class CarDetail
    {
        public Car Car{ get; set; } 
    }

Open in new window


CarsController
public ActionResult Detail(int id)
        {
            var car = Database.Session.Load<Car>(id);
            if (car == null || car.IsDeleted)
                return HttpNotFound();

            return View(new CarDetail
            {
                Car = car
            });

        }

Open in new window


What I would like to use is something like this but only using the id parameter - how do I do this???

routes.MapRoute("Cars", "cars/detail/{id}-{make}-{model}-{year}", 
                new { controller = "Cars", action = "Detail", id = UrlParameter.Optional},
                new[]{"BarMotors.Controllers"}
                );

Open in new window

swgdesignAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Personally, I find attribute routing easier to deal with than trying to set up the route table manually. If you are using the newer versions of ASP.NET MVC, and you don't have any issues with switching, then I would highly recommend switching to attribute routing.

With attribute routing your route would be implemented as:

[Route("cars/detail/{id}-{make}-{model}-{year}")]
public ActionResult Detail(int id)
{
    var car = Database.Session.Load<Car>(id);
    if (car == null || car.IsDeleted)
        return HttpNotFound();

    return View(new CarDetail
    {
        Car = car
    });

}

Open in new window


The only thing that troubles me is that you're not actually using the make, model, or year items anywere--i.e. you don't have any parameters defined as such. So what would the point of SEO-ifying be here?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
swgdesignAuthor Commented:
They're passed in the url so when indexed by Google they are searchable
0
swgdesignAuthor Commented:
I like the idea of attrbute routing and it is less cumbersome than native routing, will I need to make any changes to my ViewModels or do these stay the same and it is only the controller AR that changes?

THanks
0
käµfm³d 👽Commented:
Only the controller changes.
0
swgdesignAuthor Commented:
I had no idea about this feature, but it will make my routing so much simpler, and I can leave the default route intact to pick up any stragglers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.