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 18:56

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-10 18:56

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-10 18:56

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

查看更多
做自己的国王
5楼-- · 2019-01-10 18:57

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.

查看更多
Emotional °昔
6楼-- · 2019-01-10 18:58

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

查看更多
淡お忘
7楼-- · 2019-01-10 18:58

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

查看更多
登录 后发表回答