在 MVC 5 中创建了一个新视图,在 HTTP 404 中打开新视图结果 [英] Created a new view in MVC 5, opening the new view results in HTTP 404
问题描述
当我打开默认的 MVC 视图(索引和创建)时,它工作正常,我可以添加数据并从数据库中获取数据.当我创建一个新视图时它不起作用.它获取 Http 404.即使我复制创建视图也是如此,只是使用不同的文件名.
When I open the default MVC views(index and create) it work fine, I can add data and get data from the database. When I create a new view it don't work. It gets Http 404. This is even if I duplicate the create view, only with a different file name.
错误信息:
/"应用程序中的服务器错误.无法找到该资源.描述:HTTP 404.您要查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用.请检查以下网址并确保其拼写正确.
我遵循了一些有类似问题的人的论坛建议,但没有用.
I have follow some forum suggestions from people with similar problems, but it don't work.
我使用 MVC 5、EF6、VS2013,我的数据库是 MS-sql.
I use MVC 5, EF6, VS2013 and my DB is a MS-sql.
感谢任何为我指明正确方向的建议.谢谢!
Any suggestions that point me in the right direction is appreciated. Thanks!
推荐答案
您应该通过 action 方法访问视图.因此,如果您在 ~/Views/Home/AboutMe.cshtml
中创建了新视图,则应在 HomeController
中添加这样的操作方法.
You should be accessing the view through an action method. So If you created your new view in ~/Views/Home/AboutMe.cshtml
, You should add an action method like this in your HomeController
.
public class HomeController : Controller
{
public ActionResult AboutMe()
{
return View();
}
}
现在您可以像http://yourServerName/yourAppName/Home/AboutMe
如果您想在不同的控制器中使用您的操作方法,您可以指定完整的视图路径.例如:如果您想将操作方法添加到您的帐户控制器,
If you want to have your action method in a different controller, You can specify the full view path. Ex : IF you want to add the action method to your Account controller,
public class AccountController : Controller
{
public ActionResult AboutMe()
{
return View("~/Views/Home/aboutme.cshtml");
}
}
这篇关于在 MVC 5 中创建了一个新视图,在 HTTP 404 中打开新视图结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!