Reputation: 1076
I am practicing ASP.NET MVC using vs 2013, and writed a simple project, with one controller and view.
controller code:
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult ShowMyHome()
{
return View("MyHome");
}
}
"MyHome" view Code:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Hello World</title>
</head>
<body>
<div>
Welcome my first MVC page
</div>
</body>
</html>
RouteConfig code:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home",
url: "Home",
defaults: new { controller = "Home", action = "ShowMyHome", id = UrlParameter.Optional }
);
}
}
when i run and set url line on "http://mySite/Home/ShowMyHome", it works well. but when i run and set url line on "http://mySite/Home", i get a error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
It looks like the "Home" routing doesn't work.
Upvotes: 0
Views: 3847
Reputation: 1557
You will need to create a view for Index. In the Controller, right-click on Index() and choose "Add View".
Then, complete the wizard for how you want that view to behave.
Edit: Based on the answers I'm seeing, I believe there may be a small misunderstanding. You should not need to create a new route for every Controller you make. You should generally use the default route and use Index as your home page. Custom routes tend to come into play when you need to provide more information to your controller than just /action/id.
Upvotes: 0
Reputation: 8991
Your Home
route isn't being reached because when navigating to ~/Home
the default route is matched, given that your controller name is HomeController
.
If your desired result is to map ~/Home
to ShowMyHome()
explicitly then you will need to move your custom route above the default one.
You don't need to define a separate route for each controller actions you create, 9 times out of 10, the default route is fine.
Upvotes: 3
Reputation: 506
You still have the default route pointing to the 'Index' view and action, which doesn't exist. Place your custom route before the default route and your problem should be solved.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "Home",
defaults: new { controller = "Home", action = "ShowMyHome", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Upvotes: 2
Reputation: 197
routes.MapRoute(
name: "Home",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "ShowMyHome", id =UrlParameter.Optional }
);
Upvotes: 1
Reputation: 14418
The problem isn't routing, it's that ASP.NET can't find your Index.cshtml
view. You need to add a view for Index().
Upvotes: 2