3

This is how I think that this works

http://stackoverflow.com/questions/12333706/how-to-bla-bla-question

I think that there should be a controller action and link

@Html.ActionLink("how-to-bla-bla-question", "Questions", "Controller", new{questionId=12333706})
...
public ActionResult Questions(questionId)
{
    ...
}

Normally url should be

http://stackoverflow.com/questions/12333706/

Now is this handled by javascript or there is some other method how to make url with ID and title after it?

1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

3

I have done exactly that like this.

Global.asax snippet:

routes.MapRoute(
        "GotoPostOrPage",
        "p/{dataItemTypeId}/{dataItemId}/{ignored}", // This allows you to append a random slug if you like
        new { controller = "DataItem", action = "Details", ignored = UrlParameter.Optional }
        );

And then something like this in the controller:

public PartialViewResult Details(int dataItemTypeId, int dataItemId)
{
    IDataItemView dataItem = _dataItemService.SelectDataItem(dataItemTypeId, dataItemId);
    DataItemViewModel vm = MappingFunctions.DataItemToViewModel(dataItem);

    return PartialView("_Details", vm);
}

Note that this allows you to append a random slug which is exactly what stackoverflow does. However, stackoverflow then corrects your slug to the one stored in the database. This would be trivial to implement.

Also note that I have shown you my real code; you'd obviously need to tweak it a little bit (but not much) to fit your scenario. The important point is to append an optional url parameter in the route table. In my case I ignore it completely, which is actually what SO does too on the way in, hence you can navigate to this daft URL and still get here: https://stackoverflow.com/questions/12051094/i-like-to-eat-cake

Community
  • 1
  • 1
Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
  • Thanks, but how I can set a value to {ignored}? Do I need to add 'ignored' as a parameter in my action or I can set it some other way? For example action have only 'categoryId' parameter, do I need another parameter in that action or can get name of category from db based on ID and set it title to {ignored}. – 1110 Aug 21 '12 at 17:40