Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC – Resource Cannot be found error

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);     }     }  } 
like image 782
LCJ Avatar asked Feb 15 '12 12:02

LCJ


People also ask

How do I fix resource Cannot be found?

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.

What is HTML ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.


1 Answers

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

like image 95
greg84 Avatar answered Oct 17 '22 06:10

greg84