ASP.Net MVC - 资源找不到错误

72
我完全不了解ASP.Net MVC。我刚在Visual Studio 2010中创建了一个MVC3项目,视图引擎是razor。当我运行应用程序时,在浏览器中显示出正确的结果。URL是http://localhost:4163/。然后我将Index.cshtml文件设置为启动页,位于~\Views\Home文件夹内。然后当我运行应用程序时,URL变成了http://localhost:4148/Views/Home/Index.cshtml,并且显示资源无法找到。我该怎么做才能纠正它?URL映射在哪里进行?

Global.asax文件:

using System.Web.Mvc;
using System.Web.Routing;

namespace TEST
{

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    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
        );

    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }
    }
 }

1
请发布您的 global.asax 文件。包括以下方法:public static void RegisterRoutes(RouteCollection routes)。 - Yorgo
1
右键单击您的项目>属性。在Web选项卡下的StartAction部分中设置StartUrl。 - Yorgo
1
你知道你有不同的端口吗?(来自你的代码)将你的端口设置为特定的端口,你就不应该有问题 :) :: 同时你的默认 URL 应该是: "Home/Index" - cpoDesign
21个回答

104

URL映射或“路由”由位于ASP.NET MVC网站根目录下的Global.asax处理。

当您点击“设置为起始页”时,它会更改项目设置,以相对于应用程序根目录查找该文件。但是,在MVC中,到达索引页面的默认路由实际上是http://localhost:4163/Home/Index - 阅读类似这个链接,以了解路由如何工作。

要“修复”项目,以便它现在试图(但失败)直接导航到视图,请右键单击该项目并选择“属性”,单击“Web”选项卡,选择“具体页面”,并保留文本框为空。现在,当您开始调试时,它应该再次转到主页-查看全局.asax中RegisterRoutes方法中的默认路由参数,以查看原因。


1
谢谢,我创建了一个空的MVC应用程序,然后手动添加了一些内容。这解决了我的最后一个错误。 - BoundForGlory

27
请确保您在控制器文件夹中创建了一个名为HomeController.cs的类。

4
那是我的问题,我有一个名为"Default"的控制器,所以我把controller = "Home"改成了controller = "Default",之后它就可以工作了。 - M3NTA7
或者确保页面名称与控制器中的方法名称相同(不要拼错) :-) - Honza P.

12
在我遇到的类似问题中,我的Action拥有一个HTTP "POST"属性,但我试图打开页面(默认情况下为"GET")。因此,我不得不创建一个HTTP "GET"版本。

8

令人难以置信的是,我不小心从控制器中删除了public关键字!

enter image description here

我想这对其他人没有帮助,但你永远不知道...


我犯了这个错误,花了几个小时看其他东西! - undefined

6
我也遇到了同样的html-404错误:
资源无法找到。描述:HTTP 404。您要查找的资源(或其某个依赖项)可能已被删除,名称已更改或暂时不可用。请检查下面的URL,并确保拼写正确。
但仔细检查后,我发现我将控制器的名称留为“Default1Controller”,而没有更改为“HomeController”。当我做出更改并调试应用程序后,它起作用了。希望如果您有同样的问题,这会对您有所帮助。

5

按照以下步骤运行你的asp.net mvc应用程序。

  • Controller creation
    Right click on controllers folder and add controller. Note - don't remove Controller postfix. Its ASP.NET MVC conventions.
    Give name to controller as "DemoController" and you can see "Index" action is present in "DemoController". "Index" is default action present when you will create a controller.

  • View Creation
    Right clicking on "Index" action name and keep default view name i.e. "Index" and click on "Add" button.

  • Route Setting
    Open Global.asax file, then you will see the default setting of route is mapped to "Home" controller and "Index" action change it to "Demo" controller and "Index" action. URL mapping is written in Global.asax file. When you open Global.asax file then you will see below setting:

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    

    Change it into:

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Demo", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    
运行你的应用程序,你可以看到你的应用程序正常工作。

4

您无法在asp.net mvc中设置默认页面。
转到global.asax.cs并查看路由的定义。 默认路由指向HomeController的Index方法。
您最好观看一些有关asp.net mvc的短片或尝试找到nerd dinner tutorial,这将使您快速熟悉该框架。

我认为关于教程的最佳答案已经作为回答提供给了这个问题:

ASP.NET MVC Quick Tutorials


1
是的!不要在MVC项目中使用那个功能 - 它不起作用。相反,在Global.asax文件中为默认路由指定参数默认值。 - greg84

3

我之前在使用VS 2012时遇到过类似问题。通过点击“生成”>“重新生成”解决了该问题。


2
在ASP.NET MVC中,您不能使用“设置为起始页”的选项,因为MVC视图不是独立的,就像Web Forms页面一样。它们只是用于显示模型的模板文件。它们没有处理HTTP模块。所有Web请求都应通过控制器操作传递,您不能直接请求视图。

1
(MVC 5):更改RouteConfig.cs,包括以下两个routes
            routes.MapRoute(
            name: "DefaultWithLanguage",
            url: "{language}/{controller}/{action}/{id}",
            defaults: new { language = "fa", controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new {language= "[a-z]{2}"}
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { language = "fa", controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

当路由的语言部分未指定时,它不会将不符合正则表达式"[a-z]{2}"的控制器名称误认为是语言部分,并替换默认语言并重定向到路由的其余部分...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接