I am having issues with an asynchronous ASP.Net MVC controller method. I have a controller with one view that returns on a HttpGet to '/home' route. It works fine synchronously, but i wanted to test some of my database functionality so i changed it to asynchronous and added some code to write a model to my database.
[RoutePrefix("home")]
public class HomeController : AsyncController
{
[HttpGet]
[Route("")]
public async Task<ActionResult> IndexAsync()
{
// DEBUGGING - TEST WRITING USER TO DB
var user = new UserModel(Guid.NewGuid().ToString(), "test", "test", "[email protected]", "tester");
await user.WriteToDatabase();
// DEBUG END
return View("Index");
}
}
If I remove the lines between the comments the view is returned just fine, however, if I don't remove the lines between the comments, I get the following error:
System.InvalidOperationException
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.Web.Mvc.
I have verified that the view exists in the path "~/Views/Home/Index.cshtml" and because the page is loaded just fine in the synchronous version I know that the view's syntax is correct. The database write operation is successful but the view just cant be found for some reason.
I just don't know why turning the method asynchronous would suddenly result in a razor view engine error. Any help is appreciated.
Index
and notIndexAsync
. that way you could also juet doreturn View();
as apposed to what you are currently doing. The so-called nameing convention for async does not apply to action names. It causes the same problem you are encountering. – NkosiAsynController
and just have your controller inherit from plainController
which can handler async actions. – Nkosi