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);
}
}
}
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.
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
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 tohome/
orhome/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.
I had the same sort of issue, except I was not using the name
Index.cshtml
for myView
, I made it for exampleLandingPage.cshtml
, my problem was that theActionResult
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 myView
:LandingPage.cshtml
I am new to MVC, so perhaps this will help someone else who is new and struggling with the same thing I did.
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
Make sure you created a HomeController.cs class in your controller folder.