user70192
user70192

Reputation: 14204

Route to View in Root in ASP.NET MVC

I have an ASP.NET MVC 3 app. I kind of understand routes, but then sometimes I get stuck on something. Currently, I have a file structure like the following:

/Controllers
  Child1Controller.cs
  Child2Controller.cs
  RootController.cs
/Views
  Index.cshtml
  Test.cshtml
  Child1
    Index.cshtml
    Item.cshtml
  Child2
    Index.cshtml
    Item.cshtml

I can successfully get html from my views in Child1 and Child2. By visiting /Child1/Index or /Child2/Index in my browser. However, I can't figure out how to just use /Index to see the contents of Index.cshtml that is in the /Views directory. How do I wire that up?

Thank you!

Upvotes: 3

Views: 5926

Answers (2)

Tommy
Tommy

Reputation: 39807

In order to access views, they need to be associated with a controller/action in MVC. For your Child1/Index and Child2/Index, you have code in your Child1 and Child2 controllers similar to the following:

public ActionResult Index(){
    return View();
}

For the views that you are asking about, you can do one of two things.

1: You can create a Root folder and move those views into that folder. When returning a view from an ActionMethod, MVC will first look in the Views folder for a folder that is the same name as the controller ("Root") and in there, look for a View that corresponds to the ActionName. If it cannot find one there, MVC will then look in the Views/Shared folder. If it cannot find it there, an error is thrown. So, in your rootcontroller.cs file, create the following action methods:

public ActionResult Index(){
        return View();
    }

public ActionResult Test(){
        return View();
    }

2: If you really really want to keep your folder structure the way that it is, you can specify exactly where the view is that you want the action to return (can be used to return a view that is not the same name as your action method as well). Change those action methods in your rootcontroller.cs file to specify where the view is that you want to return for that action:

public ActionResult Index(){
        return View("~/Views/Index.cshtml");
    }

public ActionResult Test(){
        return View("~/Views/Test.cshtml);
    }

Note that both of these methods assume that you have modified the default route to use "Root" as the default controller as out of the box, it is the "Home" controller. Now, with either of these two methods, you can use the following:

www.yoursite.com -> Returns Root/Index
www.yoursite.com/Root/Test -> Returns Root/Test
www.yoursite.com/Root/Index -> Returns Root/Index

Upvotes: 4

lavrik
lavrik

Reputation: 1474

Just /Index will route to default controller and it's Index action specified in route config (usually it's in Global.asax.cs). An appropriate action of this default controller, however, will return ViewResult that will try to find the view into "controller's" folder. So if your default controller is RootController the view must be located at Root/Index. Also view is searched in shared folder, but not root one anyway. If you want to change this order try this link

Upvotes: 1

Related Questions