3

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.

2
  • Although performing async actions you should leave the name of the action as Index and not IndexAsync. that way you could also juet do return 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. Commented Jan 1, 2018 at 21:54
  • You should also not use AsynController and just have your controller inherit from plain Controller which can handler async actions. Commented Jan 1, 2018 at 21:59

1 Answer 1

2

AsyncController Class Provided for backward compatibility with ASP.NET MVC 3

Although performing async actions you should leave the name of the action as Index and not IndexAsync. that way you could also juet do return View(); as apposed to what you are currently doing. The so-called nameing convention for async does not apply to action names.

You should also not use AsynController and just have your controller inherit from plain Controller which can handler async actions.

Remarks: The Controller class in ASP.NET MVC 4 and higher supports the asynchronous patterns.

[RoutePrefix("home")]
public class HomeController : Controller {
    //Matches GET home
    [HttpGet]
    [Route("")]
    public async Task<ActionResult> Index() {
        // 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();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the reply. I implemented the changes you suggested but I am still seeing the same error, unfortunately. I have verified that the index exists in the correct place in my directory structure so I know it's there I just have no idea why it can't seem to find the Index.cshtml file.
I ended up resolving the issue, I'm not sure what the actual problem is still though. I was working on a macbook pro using Visual Studio for Mac when I ran into the issue, so i changed over to a windows machine and I no longer have the same problem, it works as expected. It seems to only be an issue when developing on a macOS system.
@JakeLeveroni yes that config is correct. I was on my mobile so did not see your comments till now.
@JakeLeveroni Just for troubleshooting. Try upgrading to MVC5 and see if you get the same problem with the different platforms.
@JakeLeveroni I'm a bit late to the party, but see here - Mono doesn't seem to support async/await with MVC yet. I found this because I'm also having the same problem on a Macbook Pro, but unfortunately no indication of when it will be implemented..
|

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.