1
votes

I can't work out how to direct asp.net mvc to the desired controller for a request to my site's root.

I've tried lots of different entries in RegisterRoutes including:

routes.MapRoute(
     "MyHome",
      "",
      new { controller = "MyController", action = "Index", id = UrlParameter.Optional }
 );

The error I get is:

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

How can I get web site root requests going to the desired controller?

Update: Still couldn't fix the issue so created a new MVC project which worked out of the box. I did rename the original project a couple of times so perhaps that screwed things up somewhere.

2

2 Answers

0
votes

For MyController to fire, your URL should look like (based on current routing) http://localhost/MyController.

Looking at your error, I am getting the feeling that you started with empty MVC project template. Is that the case?

0
votes

Based on the error message...

  1. It is going to the controller called HomeController. So, I'm guessing you have another route (maybe the default?) that directs it to the HomeController?

  2. The error message is about the view not the controller. That is a different issue from routing.

The simplest way to achieve what you are asking would be to edit the default route that was created when you created the MVC3 project.

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "MyController", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);