tags:

views:

62

answers:

3

Hey, one more newbie here, just playing around with .NET MVC. My main task is to have a few semi-static pages on URLs like:

  • /about/
  • /about/contacts/
  • /about/jobs/

I'm using a controller for that called Static and have the following route attached:

routes.MapRoute(
  "About",
  "about/{id}",
  new { controller = "Static", action = "Index", id = UrlParameter.Optional }
);

It seems to work fine as I have the Static controller with the Index method which uses a switch statement to identify which page has to be viewed. I use the RedirectToAction() function to call other actions of the Static controller in order to display pages with other views. My views are:

  • /Static/About.aspx
  • /Static/Contacts.aspx
  • /Static/Jobs.aspx

This method seems to work fine, but what I don't like about it is the redirect, so browsing to /about/contacts I get a redirect to /Static/Contacts which is not what I'd really like to see in the URL.

So my question is - what is the correct way of doing this? And is there a way to explicitly call a certain view from my Index action?

Thanks, ~ K.

+2  A: 

Don't do the redirect. Instead of using a switch statement within the Index action, have a separate action for each page (ie. About, Contacts, Job) each with their own view.

Your Static controller could look something like this:

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

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

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

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

And if you needed to do any processing specific to Contacts or Jobs, it can be done within their respective actions.

To explicitly call a certain view:

return View("ViewName");

There are seven overloads for the View() method. A number of which allow you to pass the Model:

return View("ViewName", Model);
JungleFreak
Thanks, your answers were correct, and perhaps my question wasn't. I used your suggestions to create my Static controller and had the About, Contacts and Jobs actions, routed them and it all seems to work fine. Only concern is the upper-cases in the URL ;)
kovshenin
Perhaps one more question about explicitly calling a view is, how to call a View explicitly AND pass it some View Model?
kovshenin
@kovshenin: I'm sorry. It should be `return View("ViewName")`. I'll edit the answer above. As far as the upper-case URLs, why are you concerned? I am assuming this is running on IIS, which has case-insensitive URLs. `http://yourdomain.com/About` will be treated the same as `http://yourdomain.com/about`.
JungleFreak
+2  A: 

I'd recommend moving away from Static, and have an About controller. Within that controller, one method per page.

public ActionResult About() 
{
   return View ("About");
}

//Jobs() and Contacts() follow the same pattern

3 routes to match:

routes.MapRoute(
  "Jobs",
  "about/jobs",
  new { controller = "About", action = "Jobs" }
);

routes.MapRoute(
  "Contact",
  "about/contact",
  new { controller = "About", action = "Contact"  }
);

routes.MapRoute(
  "About",
  "about/",
  new { controller = "About", action = "About" }
);
p.campbell
you dont need those 3 routes, the default route will work perfectly.
Stefanvds
I agree with the post and the above comment.
Blankasaurus
+1  A: 

you cant return views from a different controller and have the first controller in the URL.

the only way is use your about controller.

so place your logic in your about controller.

i have the same for myself in my Admin controller. it's just a page with some static links, and some extra pages.

in the index.aspx i have

<ul>
    <li>
        <%= Html.ActionLink("Evaluaties", "Evaluaties", "Admin")%></li>
    <li>
        <%= Html.ActionLink("ONAS aanbieders", "Index", "ONASAanbieder")%></li>
    <li>
        <%= Html.ActionLink("ONAS aanbod", "Index", "ONASAanbod")%></li>
    <li>
        <%= Html.ActionLink("Helpbox", "Index", "HelpBox")%></li>
</ul>

in the controller I have

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

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

this works like you described, no need to alter the routes. obviously i have an Evaluaties.aspx in my Admin folder in the Views folder.

gives me this URL: http://localhost:50152/Admin/Evaluaties

Stefanvds