可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Error like:The view 'LoginRegister' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/MyAccount/LoginRegister.aspx
~/Views/MyAccount/LoginRegister.ascx
~/Views/Shared/LoginRegister.aspx
~/Views/Shared/LoginRegister.ascx
~/Views/MyAccount/LoginRegister.cshtml
~/Views/MyAccount/LoginRegister.vbhtml
~/Views/Shared/LoginRegister.cshtml
~/Views/Shared/LoginRegister.vbhtml
Actually my page view page is ~/Views/home/LoginRegister.cshtml
so what i do
and my RouteConfig
is
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "MyAccount", action = "LoginRegister", id = UrlParameter.Optional } ); } }
回答1:
Be careful if your model type is String because the second parameter of View(string, string) is masterName, not model. You may need to call the overload with object(model) as the second paramater:
Not correct :
protected ActionResult ShowMessageResult(string msg) { return View("Message",msg); }
Correct :
protected ActionResult ShowMessageResult(string msg) { return View("Message",(object)msg); }
OR (provided by bradlis7):
protected ActionResult ShowMessageResult(string msg) { return View("Message",model:msg); }
回答2:
Problem:
Your View
cannot be found in default locations.
Explanation:
Views should be in the same folder named as the Controller
or in the Shared
folder.
Solution:
Either move your View
to the MyAccount
folder or create a HomeController
.
Alternatives:
If you don't want to move your View
or create a new Controller
you can check at this link.
回答3:
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.
回答4:
In your LoginRegister action when returning the view, do below, i know this can be done in mvc 5, im not sure if in mvc 4 also.
public ActionResult Index() { return View("~/Views/home/LoginRegister.cshtml"); }
回答5:
Check whether the View (.ASPX File) that you have created is having the same name as mentioned in the Controller. For e.g:
public ActionResult GetView() { return View("MyView"); }
In this case, the aspx file should be having the name MyView.aspx instead of GetView.aspx
回答6:
This could be a permissions issue.
I had the same issue recently. As a test, I created a simple hello.html page. When I tried loading it, I got an error message regarding permissions. Once I fixed the permissions issue in the root web folder, both the html page and the MVC rendering issues were resolved.
回答7:
I got this error because I renamed my View (and POST action).
Finally I found that I forgot to rename BOTH GET and POST actions to new name.
Solution : Rename both GET and POST actions to match the View name.
回答8:
If the problem happens intermittently in production, it could be due to an action method getting interrupted. For example, during a POST operation involving a large file upload, the user closes the browser window before the upload completes. In this case, the action method may throw a null reference exception resulting from a null model or view object. A solution would be to wrap the method body in a try/catch and return null. Like this:
[HttpPost] public ActionResult Post(...) { try { ... } catch (NullReferenceException ex) // could happen if POST is interrupted { // perhaps log a warning here return null; } return View(model); }
回答9:
Check the build action of your view (.cshtml file) It should be set to content. In some cases, I have seen that the build action was set to None (by mistake) and this particular view was not deploy on the target machine even though you see that view present in visual studio project file under valid folder
回答10:
If you've checked all the things from the above answers (which are common mistakes) and you're sure that your view is at the location in the exceptions, then you may need to restart Visual Studio.
:(
回答11:
My work has some policies in place that does not make NuGet work at all, and as a result, my solution lost its references to the microsoft.aspnet.mvc DLL. However, once I added the DLL to the references manually, I was able to render the page.