简体   繁体   English

asp.net无法找到资源?

[英]asp.net The resource cannot be found?

Well, i just created a new asp.net empty project with MVC and WebApi included 好吧,我刚创建了一个包含MVC和WebApi的新asp.net空项目

在此输入图像描述

The project is created and compiles successfully however, when i add anything in the views folder and try to view it in the browser i get IIS message HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 该项目已创建并成功编译,但是当我在views文件夹中添加任何内容并尝试在浏览器中查看它时,我会收到IIS消息HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

If you wanna reproduce such issue just do what i did and it will definitely occur (except if i am on some kind of drug that impaired my vision :D) 如果你想重现这样的问题,那就做我做过的事情肯定会发生(除非我在某种药物上影响了我的视力:D)

Are you sure you have added the controller that will intercept the request and will render the appropriate view? 您确定已添加了拦截请求的控制器并将呈现相应的视图吗? You cannot route the request directly to something you have inside your view folder. 您无法将请求直接路由到视图文件夹中的内容。 You need to have a controller and inside a controller you will have to add an action method that will you can use to return the view. 您需要一个控制器并在控制器内部,您必须添加一个可用于返回视图的操作方法。

For ex. 对于前者 you have a controller named Home and in that you have an action method named Index, so when you will hit the following url: localhost/localApplication/Home/Index 你有一个名为Home的控制器,你有一个名为Index的动作方法,所以当你点击下面的url:localhost / localApplication / Home / Index

It will execute the Index method of home controller and will return the view if it is designed to return a view else anything else and the correct method to add a view for an action is to right click inside the action source and select add view, which in above case will add the view in following location Views>Home>Index.cshtml (if its razor view) I hope this helps. 它将执行home控制器的Index方法,如果它被设计为返回视图,则返回视图,其他任何东西和添加视图的正确方法是右键单击动作源并选择add view,在上面的情况下将视图添加到以下位置Views> Home> Index.cshtml(如果它的剃刀视图)我希望这有帮助。

it won't work because if you are using MVC you need also add the controller. 它不起作用,因为如果你使用MVC,你还需要添加控制器。 and then add a method returning the view you want to render example you add a controller named Home and if your Action in controller is as below 然后添加一个方法返回要渲染的视图示例您添加一个名为Home的控制器,如果您在控制器中的Action如下所示

public ActionResult Index() { return View(); }

this will render the View named Index.cshtml which you have added in Views>>Home folder,you will need to add controller to Controller folder 这将呈现您在Views >> Home文件夹中添加的名为Index.cshtml的View,您需要将控制器添加到Controller文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM