I’m a web developer new to using the MVC3 framework. We’re building a site that implements a lot of sub folders for different segments of our audience. This routing concept is throwing a wrench in our structure for SEO.
In my global.asax file under the routing section we have:
routes.MapRoute("test", "test/{testFirst}/{testSecond}",
new { controller = "test", action = "RouteTest", testSecond = UrlParameter.Optional });
and in my controller we have:
public ActionResult RouteTest(string testFirst, string testSecond)
{
return View(testFirst, testSecond);
}
When I run the site and try to go to /test/test/index it won’t pull up the view. It’s stuck looking for test.cshtml which doesn’t exist because it’s a folder not a file.
Any ideas on to how make nested folders work?
EDIT:
Here’s a branch of the structure we want and maybe it will help with what I’m trying to accomplish.
This is kind of hard to show but it should get the idea across. We have 5 different audiences that come to the site. I broke down 1 audience and what the flow of that audience is.
Not all segments will have products some are just content other segments have that 3rd level and have products to view
audience
- segment
- segment
- products
- segment
- products
- segment
This is the basic structure that we want the URLs to take
domain.com/audience/segment/products/(productsname)
Suggestions on how to make this possible
You are using the wrong overload for the
View()method. Here’s what you’re using when you callView(testFirst, testSecond):MSDN Reference.
By putting “test” for the
viewName, you’re telling the Controller to render a View calledTest(test.cshtml). Which you don’t have.It sounds to me like you are trying to correlate WebForms with MVC. It is not the same, and you are seeing a prime example with routing. ASP.NET MVC doesn’t work off of the NTFS structure (folders and files). It relies on routing through route definitions.
If you are looking to render the View “RouteTest”, then do something like this:
This will render the “RouteTest” view and in your dynamic object
ViewBagyou will have access to two properties:testFirstandtestSecond. In your view you can pull those values. (Although I highly recommend strongly-typed Views using a ViewModel)Example Solution
ViewModel
Controller
Strongly-Typed View