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", "test@test.com", "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.

Jake Leveroni
  • 102
  • 1
  • 9
  • 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. – Nkosi Jan 01 '18 at 21:54
  • You should also not use `AsynController` and just have your controller inherit from plain `Controller` which can handler async actions. – Nkosi Jan 01 '18 at 21:59

1 Answers1

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", "test@test.com", "tester");
        await user.WriteToDatabase();
        // DEBUG END

        return View();
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 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. – Jake Leveroni Jan 01 '18 at 22:48
  • 3
    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. – Jake Leveroni Jan 01 '18 at 23:27
  • i use attribute routing so its small but here it is: `public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); } }` – Jake Leveroni Jan 01 '18 at 23:29
  • 1
    @JakeLeveroni yes that config is correct. I was on my mobile so did not see your comments till now. – Nkosi Jan 02 '18 at 00:02
  • 2
    @JakeLeveroni Just for troubleshooting. Try upgrading to MVC5 and see if you get the same problem with the different platforms. – Nkosi Jan 02 '18 at 00:03
  • 4
    @JakeLeveroni I'm a bit late to the party, but see [here](https://stackoverflow.com/questions/49848492/how-to-use-async-in-asp-net-mvc-for-mono) - 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.. – brads3290 Feb 13 '19 at 05:04
  • Like @JakeLeveroni me too. I'm changing platform from Mac to WIn and works perfectly!! Visual Studio for Mac is not still mature!!! :( – Blasco73 Feb 13 '20 at 10:42