找不到资源。 ASP.NET MVC3
我不知道为什么在调试项目后会出现此错误,即使代码是默认的。
控制器
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
视图
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
Hi
</div>
</body>
不知何故,或者更确切地说,在调试之后,请求的 URL 始终是 /Views/Home/Index.cshtml,但通过浏览器访问 Home 是可以的。 (http://localhost:58323/home)
我用谷歌搜索,解决方案提示问题出在全局。但这很奇怪,我不记得对此进行过任何更改。
全球
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
感谢任何帮助。谢谢
I have no idea why this error is occurring after debugging the project even though the codes are default.
Controller
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
Hi
</div>
</body>
Somehow or rather, after debugging, the Requested URL is always /Views/Home/Index.cshtml but accessing Home via browser is fine. (http://localhost:58323/home)
I googled and solution hints that the problem lies in global. But thats weird, I dont remember making any changes to it.
Global
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Appreciate any help. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您只需设置视觉工作室设置,以便将视图设置为起始页。右键单击该项目并转到属性,然后转到 Web 选项卡。是否选择了“特定页面”单选按钮并将“Views/Home/Index.cshtml”设置为值?将其更改为使用起始网址。就我个人而言,我不喜欢让调试器启动浏览器并使用“不要打开页面”。
I think you just have your visual studio settings set so that the view is set to the start page. Right click on the project and go to properties, then to the web tab. Is the 'specific page' radio button selected with 'Views/Home/Index.cshtml' set as the value? Change it to use a start url. Personally I prefer not to have the debugger start up a browser and use Don't open a page.
右键单击您的网络项目 ->属性-> Web
检查您是否已将启动操作设置为“特定页面”且该字段中没有任何值。
我的猜测是您已将启动操作设置为当前页面。
Right click your web project -> Properties -> Web
check that you have the start action set to Specific Page with no value in the field.
My guess is that you have the start action set to current page.
如果 Views 文件夹中的文件夹结构层次结构不正确,甚至可能会导致此错误。
如果您通过右键单击“视图”文件夹来添加视图。添加的新视图可能会错误地放置在文件夹层次结构中。
解决问题的方法是:
考虑一个名为index.ascx 的视图,它应该链接到名为HomeController 的控制器。
在 Views 文件夹下应该有一个文件夹名称 Home(与 Controller 相关)和
index.ascx 应驻留在主文件夹中。
添加视图的最佳方法是在公共方法旁边右键单击,这将在上下文菜单中显示“创建视图”选项。如果您以这种方式创建视图,则会自动创建文件夹层次结构。
This error might even cause if the hierarchy of the folder structure is incorrect in Views folder.
If you are adding views by right-click on Views folder.The new view added might be incorrectly placed in the folder hierarchy.
The way to troubleshoot the problem is:
Consider a view named index.ascx which is supposed to be linked to a Controller named HomeController.
Under the Views folder there should be a folder name Home (relating to the Controller) and the
index.ascx should reside in Home folder.
The best possible way to add view is to right-click just beside the public method which will show a Create View option in context-menu.If you crate view in such manner then folder hierarchy is automatically created.