ASP.Net MVC – Resource Cannot be found error

2019-01-10 18:38发布

I am completely new to ASP.Net MVC. I just created an MVC3 project in Visual Studio 2010. The view engine is razor. When I just ran the application it gave the proper result in the browser. The URL is http://localhost:4163/ . Then I applied “Set as Start Page” to Index.cshtml inside ~\Views\Home folder. Then when I ran the application the url became http://localhost:4148/Views/Home/Index.cshtml and it said the resource cannot be found. What do I do to correct it? Where is the url mapping done?

Global.asax file:

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

21条回答
劳资没心,怎么记你
2楼-- · 2019-01-10 19:00

To see your View like this:

http://localhost:1234/x/MyView

In the xController.cs file, under Controllers folder,

make sure you add an ActionResult method like this:

public ActionResult MyView()
{
    return View(whatever);
}

I also got this message when I wasn't referencing my model name correctly:

@using (Html.BeginForm("MyView", "MyModel", FormMethod.Get))

查看更多
唯我独甜
3楼-- · 2019-01-10 19:02

Unbelievably, I'd accidentally deleted the public keyword from the controller!

enter image description here

I imagine this will help precisely no one else, but you never know ...

查看更多
等我变得足够好
4楼-- · 2019-01-10 19:02

Just adding this one in here - if anyone is having issues where the index page on the Home Controller no longer loads, it might be because you haven't setup your StartUp Project yet. Right-click the project that you want your app to start at and set it as the startup project, and you should be good to go.

查看更多
神经病院院长
5楼-- · 2019-01-10 19:06

in the address bar on browser input like this: ControllerName/ActionMethod not input ControllerName/ViewName for example in address bar: http://localhost:55029/test2/getview cshtml file code:

@{
Layout = null; 
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyView</title>
</head>
<body>
<div>            
</div>
</body>
</html>

and cs file code:

public ActionResult GetView()
{
return View("MyView");
}
查看更多
Viruses.
6楼-- · 2019-01-10 19:07

(MVC 5): Change RouteConfig.cs to include two routes like this:

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

so when the language part of the route is not specified it does not mistake the controller names which not match regex "[a-z]{2}" and replaces default language and redirects to rest of route...

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-10 19:08

I too got the same html-404 error:

The resource cannot be found.Description: 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.

But after close examination, I found that I had left the name of the controller as Default1Controller instead of changing it to HomeController. When I made the change and debugged the app it worked. Hope it will help you if you have the same problem.

查看更多
登录 后发表回答