0

This is probably something very simple, but Googling feebly isn't getting me anywhere.

In the web application we're building, we've got Projects, and Templates. Templates are really just Projects, with the IsTemplate flag set to true. So naturally, we've got a single Project controller that handles (or will handle) both cases.

We've got one route to a New action method on the controller:

Project/New

That's just the standard {controller}/{action}/{id} route handling that one. Now, the New action method has an IsTemplate parameter. I'd like to have one route where that's passed in as false (the one above), and a second one where it's passed in as true:

Templates/New

What's the proper way to mask an arbitrary action method parameter with varying URLs like that? I tried the following, but it just confused the routing (Html.ActionLink ends up pointing at Templates/New):

routes.MapRoute(
    null,
    "Template/New",
    new { controller = "Project", action = "New", IsTemplate = true }
);

Or would it be a lot simpler for me to just split this into two action methods, and have those call a single private controller method with a hard-coded parameter value?

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
db2
  • 497
  • 1
  • 3
  • 21
  • It is ok to solve this problem like this (i am assuming you leaved the default route untouched). Another possibility would be to use [ActionName](http://stackoverflow.com/questions/6536559/purpose-of-actionname). Yet another possibility would be to create an implicit action called just template for template/new that just calls project/new(isTemplate=true). – keenthinker Oct 28 '13 at 19:26

2 Answers2

1

Seems like this might be the simplest option. I've split the single New action method into two action methods, both of which have different routes defined, and a third private method that does all the work. I'm still open to recommendations if there's a prettier way to do it.

[HttpGet]
//Action method for a new template
public ActionResult NewTemplate() {
    return New(true, null);
}

[HttpGet]
//Action method for a new project
public ActionResult New(int? TemplateProjectID) {
    return New(false, TemplateProjectID);
}

private ActionResult New(bool IsTemplate, int? TemplateProjectID) {
    //Assorted code follows
db2
  • 497
  • 1
  • 3
  • 21
  • I think I'll just settle for this, as it's relatively simple, and we won't need to scale it to an indefinite number of different URLs. – db2 Nov 07 '13 at 15:50
0

Perhaps RouteUrl can help you? If you named the route to TemplateNew for example, you should be able to create an url for that:

<a href="@Url.RouteUrl("TemplateNew")">New Template</a>
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104