How to pass DateTime.Ticks as URI parameter?

How can I get this code working?

public ActionResult ABC(DateTime.Now.Ticks Id)
{
 //some stuff
 return View();
}

Your thoughts?

EDIT The whole concept is have unique URLs for each and every visitor/user.

Jon Skeet
people
quotationmark

First, the basics: when declaring a method, you need to specify the type of the parameter, which in this case would be long:

public ActionResult Abc(long id)
{
    // ...
}

When you call that (whether internally or whether it's responding to a URL, e.g. generated in Javascript), you can choose to use DateTime.Now.Ticks as the argument to do, just as you could call the method like this inside the application:

Foo.Abc(DateTime.Now.Ticks);

However some warnings:

  • There's no guarantee that callers will use that as the argument
  • Even if they did, it will be the current time in their system local time zone, which is almost certainly a bad idea
  • If you're trying to use a number of ticks as an ID, that's also a bad idea:
    • DateTime.UtcNow.Ticks will return the same value multiple times when called in quick succession
    • Multiple machines could call the method at the same time anyway

If your idea is to generate a unique ID, I'd probably just go with Guid.NewGuid()... or whatever your storage layer provides for generating unique IDs. Even keeping an internal, atomically-incremented counter on the machine has problems in terms of scaling horizontally. That's sometimes solved using a high-low ID generation strategy.

people

See more on this question at Stackoverflow