MVC routing - using part of a route to switch views

I have a series of urls that look like

/Catalog/Ajax/Update/{ViewToUpdate}?var1=a&var2=b&var3=c

      

Currently I have set up several routes - one for each {ViewToUpdate}, and what I would like to do is pass {ViewToUpdate} to my Action handler so that I can condense my code. Instead:

public ActionResult AjaxUpdateNavigation(string var1, string var2, string var3) {}

      

I would like to:

public ActionResult AjaxUpdateNavigation(string ViewToUpdate, string var1, string var2, string var3) {}

      

Here are my current routes:

routes.MapRoute(
"CatalogAjaxNavigation",
"Catalog/Ajax/Update/Navigation",
new { controller = "Catalog", action = "AjaxUpdateNavigation" }
);

      

How do I properly configure the route definition to handle both the {ViewToUpdate} string and still passing through the request?

TIA

+1


a source to share


2 answers


Here's my route:

  routes.MapRoute("TestThing", "test/{ViewToUpdate}", new {controller = "Home", action = "TestQSParams"});

      

Here's my action:

  public ActionResult TestQSParams(string ViewToUpdate, string var1, string var2)
  {
      TestQSParamsModel m = new TestQSParamsModel {var1 = var1, var2 = var2, ViewToUpdate = ViewToUpdate};
      return View("TestQSParams", m);
  }

      

Here's my model:

public class TestQSParamsModel
  {
      public string ViewToUpdate { get; set; }
      public string var1 { get; set; }
      public string var2 { get; set; }
  }

      

Here's my view:



From QS:<br />
  <% foreach(string s in Request.QueryString) 
         Response.Write(string.Format("{0}={1}<br />", s, Request.QueryString[s])); %>
  <br />
  <br />
  From Model:<br />
  <asp:Literal ID="modelvars" runat="server"></asp:Literal>

      

View code:

  protected void Page_Load(object sender, EventArgs e)
  {
      modelvars.Text = string.Format("{0}<br />{1}<br />{2}", Model.var1, Model.var2, Model.ViewToUpdate);
  }

      

My url:

/test/ThisView?var0=douglas&var1=patrick&var2=caldwell

      

Finally my result:

  From QS:   
  var0=douglas   
  var1=patrick   
  var2=caldwell

  From Model:
  patrick
  caldwell
  ThisView

      

+1


a source


routes.MapRoute(
"CatalogAjaxNavigation",
"Catalog/Ajax/Update/{ViewToUpdate}",
new { controller = "Catalog", action = "AjaxUpdateNavigation" , ViewToUpdate = (string)null }
);


public ActionResult AjaxUpdateNavigation(string ViewToUpdate, string var1, string var2, string var3) {}

      



0


a source







All Articles