-1

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.

Dawar
  • 99
  • 3
  • 14
  • You should use type instead of `DateTime.Now.Ticks`. So, how about `long Id`? – Soner Gönül Mar 05 '17 at 12:34
  • Is that unique for every visitor? @SonerGönül – Dawar Mar 05 '17 at 12:35
  • How r u calling action, what issue, add that part also – Anil Mar 05 '17 at 12:37
  • @Dawar: If it's received as a URI parameter, callers can put whatever they like on it. I could hit http://yourserver/ABC?Id=10 a hundred times, and so could someone else. You can't rely on external sources not under your control for uniqueness. – Jon Skeet Mar 05 '17 at 12:38
  • @JonSkeet how to avoid that situation? I need a hint to start learning about it. – Dawar Mar 05 '17 at 12:40
  • My answer gives several suggestions. Fundamentally you need to understand that parameters for methods like this are provided by the clients. – Jon Skeet Mar 05 '17 at 12:48

1 Answers1

4

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.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • any alternative to skip those warnings? – Dawar Mar 05 '17 at 12:38
  • 1
    @Dawar: Which warnings? The ones in my answer? You're being very unclear - but please read my complete answer carefully. I think you need to re-examine your whole understanding of how URI parameters should be treated. – Jon Skeet Mar 05 '17 at 12:39
  • @Dawar: If your fundamental goal is to get something into production quickly without caring at all about uniqueness, then you can certainly ignore all the warnings in my post. However, I would **very strongly** urge you not to do so. What's the point of asking a client to supply an ID if you can't reasonably expect it to be unique? – Jon Skeet Mar 05 '17 at 12:40
  • The whole concept is to have unique URLs for different users/visitors. – Dawar Mar 05 '17 at 12:45
  • @Dawar: But the visitor decides the URL - so how are you expecting to *force* them to be unique? If one of your visitors emails a URL to another person, you can obviously have two different users hitting that URL at the same time. Take a step back and think about what you're really trying to achieve - but Stack Overflow is *not* for ongoing discussion. – Jon Skeet Mar 05 '17 at 12:50