I am learning Async/Await in MVC5. I created this demo app but my controller action method is not getting fired. Its giving "The resource cannot be found." error. Here is the code.
_Layout.cshtml view
@Html.ActionLink("ASYNC", "GetNames", "Async")GetNames.cshtml view
@Html.ActionLink("Click Me", "GetFirstName", "Async", null, new{@class = "btn btn-primary" }) @if (ViewBag.FName != null) { <p> <label>@ViewBag.FName </label> </p> }In AsyncController.cs file
public ActionResult GetNames() { return View(); } [HttpPost] public async Task<ActionResult> GetFirstName() { var r = await GetFirstNameFromDB(); ViewBag.FName = r; return View("GetNames"); } public async Task<string> GetFirstNameFromDB() { try { var res = await Task.Run(() => { Thread.Sleep(3000); return "TestFName"; }); return res; } catch (Exception) { return "Error in getting FirstName"; } }
Now, whenever i click the action link button to call the GetFirstName, it shows The resource cannot be found. error. I'm not sure if it is related to action being async or something else.
ActionLinkproduces an<a>anchor tag. Clicking on these generates a GET request. However your action is marked[HttpPost].