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?

4
  • What control are you using? Please edit your question and add details. Commented Jan 1, 2021 at 11:35
  • I am Using NFC Reader ACS ACR122 0 . C# MVC WEB Application. Commented Jan 1, 2021 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. Commented Jan 1, 2021 at 19:25
  • Please check this post for more information ASP.net get hardware information Commented Jan 1, 2021 at 19:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.