views:

35

answers:

1

I am building my first .NET MVC web application and loving the simplicity and flexibility. I have however come to my first stumbling block.

I have a list of items and if you are not logged in you will see a preview link I would like the link to direct to something like below:

/preview/unique-slug

The view should then allow me to display the contents from the database (essentially a details page)

I am not sure how to approach this nor what I should be Google'ing as the results I got were poor.

Any pointers please?

+1  A: 

This is the way I've done this in the past:

You need to store in the database a column with the slug that each item is referred to by. Then, create a route like the following:

routes.MapRoute("PreviewLink",
    "/preview/{slug}",
    new { controller = "Preview", action = "Details" }
);

So this will call the Details method on the PreviewController like so:

class PreviewController : Controller
{
    public ActionResult Details(string slug)
    {
        var model = db.GetItemBySlug(slug);
        return View("Details", model);
    }
}

Basically, you'll get the slug as a string in your action method, query the database for the item with the corresponding slug, and display it. Essentially, we just replace an integer id with a string slug.

Hopefully that clears things up for you!

Dean Harding