The view or its master was not found or no view engine supports the searched locations

前端 未结 14 2308
挽巷
挽巷 2020-12-02 15:05

Error like:The view \'LoginRegister\' or its master was not found or no view engine supports the searched locations. The following locations were searched:

14条回答
  •  旧时难觅i
    2020-12-02 15:53

    In Microsoft ASP.net MVC, the routing engine, which is used to parse incoming and outgoing URL Combinations, is designed with the idea of Convention over Configuration. What this means is that if you follow the Convention (rules) that the routing engine uses, you don't have to change the Configuration.

    The routing engine for ASP.net MVC does not serve web pages (.cshtml). It provides a way for a URL to be handled by a Class in your code, which can render text/html to the output stream, or parse and serve the .cshtml files in a consistent manner using Convention.

    The Convention which is used for routing is to match a Controller to a Class with a name similar to ControllerNameController i.e. controller="MyAccount" means find class named MyAccountController. Next comes the action, which is mapped to a function within the Controller Class, which usually returns an ActionResult. i.e. action="LoginRegister" will look for a function public ActionResult LoginRegister(){} in the controller's class. This function may return a View() which would be by Convention named LoginRegister.cshtml and would be stored in the /Views/MyAccount/ folder.

    To summarize, you would have the following code:

    /Controllers/MyAccountController.cs:

    public class MyAccountController : Controller 
    {
        public ActionResult LoginRegister()
        {
            return View();
        }
    }
    

    /Views/MyAccount/LoginRegister.cshtml: Your view file.

提交回复
热议问题