ASP.Net MVC – Resource Cannot be found error

2019-01-10 18:20发布

问题:

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

回答1:

URL mapping or "routing" is handled by Global.asax in the root of your ASP.NET MVC site.

When you click "Set as Start Page" it changes the project settings to look for that file relative to the application root. But in MVC the default route to your index page is actually http://localhost:4163/Home/Index - read something like this to get an idea of how routing works.

To "fix" your project now that it's trying (and failing) to navigate to the view directly, right click the project and choose "Properties", click the "Web" tab and choose "Specific Page", leaving the text box blank. Now when you start to debug it should go to the home page again - look at the default route parameters to see why in the RegisterRoutes method in Global.asax



回答2:

Make sure you created a HomeController.cs class in your controller folder.



回答3:

In a similar issue I faced, my Action had an HTTP "POST" attribute, but I was trying to open the page (by default it's "GET").

So I had to create an HTTP "GET" version.



回答4:

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.



回答5:

Follow below steps to run you asp.net mvc application.

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

And run your application you can see your application in working.



回答6:

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

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



回答7:

Well you cannot set the default page in asp.net mvc.
Go to global.asax.cs and see the definition of routing. The default route points to Index method HomeController.
You'll better watch some short movies about asp.net mvc or try to find nerd dinner tutorial which will make you familiar with the framework pretty quickly.

I think the best answers about tutorials were already provided as answers to this question:

ASP.NET MVC Quick Tutorials



回答8:

In asp.net mvc you can't use option 'set as start page', because mvc views is not independent, like web forms pages. It is only template files for displaying your model. They have no processing http module. All web requests should pass through controller actions, you can't request views directly.



回答9:

I had a similar problem some time ago when using VS 2012. Solved by just Rebuilding the application by clicking Build > Rebuild.



回答10:

If you're using IIS Express. It can be that you opened another project which uses same virtual directory. When you opened 2nd solution VS remaps that IIS directory to another location. As result it won't work with different problems. I had same error as topic starter.

To fix: Close all instances of VS Studio. Open VS with project you're working & rebuild.



回答11:

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.



回答12:

(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...



回答13:

asp.net mvc project's start page is "Current Page" defaulty. Just open an server-side file and start project. in that way mvc open the default controller which is homecontroller for your project



回答14:

You can change the start page inside of your project's properties (project -> project properties). Under the web tab you can select specify page and change your start page to home/ or home/index. This will direct you to the proper page according to your routes. The address of http://localhost:4148/Views/Home/Index.cshtml doesn't work specifically because it is being handled according to your routes which state that the url needs to be http://localhost:4148/{controller}/{action} and an optional /{id}.

If you want to learn more about routing take a look here.



回答15:

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



回答16:

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


回答17:

If you have tried all of the above solutions and problem still persists then use this simple solution. Open facebook debugger from this link, type page url and click on Fetch new Scrape Information button. This will solve your problem.

The reason behind (what I think) is that facebook api saves current state of the page and shows same from its own server. if you click share button while coding and your page actually is not uploaded then facebook api could not find that page on server and saves the page state as "resource not found". Now it will keep showing same message even after you upload the page. When you fetch page from facebook debugger, it resolve everything from scratch and saves current state of the page and then your share button starts working without any change in the coding.



回答18:

I had the same sort of issue, except I was not using the name Index.cshtml for my View, I made it for example LandingPage.cshtml, my problem was that the ActionResult in my Controller was not the same name as my View Name(In this case my ActionResult was called Index()) - This is actually obvious and a silly mistake from my part, but im new to MVC, so I will fail with the basics here and there.

So in my case I needed to change my ActionResult to be the same name as my View : LandingPage.cshtml

    public ActionResult LandingPage()
    {
        ProjectDetailsViewModels PD = new ProjectDetailsViewModels();
        List<ProjectDetail> PDList = new List<ProjectDetail>();

        PDList = GetProductList();
        PD.Projectmodel = PDList;


        return View(PD);
    }

I am new to MVC, so perhaps this will help someone else who is new and struggling with the same thing I did.



回答19:

For me the problem was caused by a namespace issue.

I had updated my project namespace in my web project classes from MyProject to MyProject.Web, but I had forgotten to update the Project Properties >> Default Namespace. Therefore when I added a new controller it was using the old namespace. All I had to do was correct the namespace in the new controller and it worked.



回答20:

I had a controller named usersController , so I tried to create it the start page by changing default controller from Home to usersController like given below.

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

The solution was to change controller default value to users rather than usersController

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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


回答21:

Solved the problem by right clicking on the view I wanted as the startup page After Displaying the error "The resource you are looking for cannot be found" Simply right click on your project, go to web and select Current Page. That will solve the problem