0

I am working with ASP.NET MVC and C#. I want to fire card inserted/ejected event in my application. How would that be possible? It works perfectly fine in Windows Forms by just adding events in Form_Load(). But in ASP.NET MVC, where do I have to set these handlers ?

The Index method is :

public ActionResult Index()
{
   return View("Index");
}

The events that to be fire. Note: if I put these events in Index() before return View() then I get an error

Exception ( Async Function )

Code:

 f.CardInserted += new f.CardEventHandler(Card_Inserted);
 f.CardEjected += new f.CardEventHandler(Card_Ejected);
 f.Watch();

Card inserted / eject event handler definition:

public void Card_Inserted()
{
    try
    {
        if (f.Connect())
        {
             String x = f.GetCardUID();
             TempData["message"] =  x.ToUpper();
            //TempData["message"] = "Connection Established";
        }
        else
        {
            TempData["message"] = "Error During Connection";
        }
    }
    catch (Exception)
    {
        TempData["message"] = "Error During Try/Catch";
    }
}

public void Card_Ejected()
{
    TempData["message"] = "Card Removed";
    f.Disconnect();
}

Watch method:

public void Watch()
{
    this.RdrState = new Card.SCARD_READERSTATE();
    readername = GetReadersList()[0];
    this.RdrState.RdrName = readername;
    states = new Card.SCARD_READERSTATE[1];
    states[0] = new Card.SCARD_READERSTATE();
    states[0].RdrName = readername;
    states[0].UserData = 0;
    states[0].RdrCurrState = Card.SCARD_STATE_EMPTY;
    states[0].RdrEventState = 0;
    states[0].ATRLength = 0;
    states[0].ATRValue = null;
    this._worker = new BackgroundWorker();
    this._worker.WorkerSupportsCancellation = true;
    this._worker.DoWork += WaitChangeStatus;
    this._worker.RunWorkerAsync();
    return;
}

Where do I have to set these event handlers to work?

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Aadi
  • 1
  • What control are you using? Please edit your question and add details. – derloopkat Jan 01 '21 at 11:35
  • I am Using NFC Reader ACS ACR122 0 . C# MVC WEB Application. – Aadi Jan 01 '21 at 11:40
  • Firstly, you have to understand that web page have different behaviour than Windows Forms, the web page don't have by default access to your computer (for security and privacy reasons), and actually what you want to achieve is exactly this (access to hardware), so your web page need to notify to client (in the view) to accept a script that allow detecting and reading from your card, you have to search in this way in my opinion. – B.S. Jan 01 '21 at 19:25
  • Please check this post for more information [ASP.net get hardware information](https://stackoverflow.com/questions/16128691/asp-net-get-hardware-information) – B.S. Jan 01 '21 at 19:35

0 Answers0