ASP.Net Async MVC controller function causes Razor View Engine error

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.


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();
    }
}
链接地址: http://www.djcxy.com/p/59770.html

上一篇: PyLint,PyChecker或PyFlakes?

下一篇: ASP.Net Async MVC控制器函数导致Razor View Engine错误