1

I have a series of URLs that look like

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

Currently I've setup several routes - one for each {ViewToUpdate} and what I'd like to do is pass the {ViewToUpdate} to my Action handler so I can condense my code. Instead of:

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

I'd like:

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 set up the route definition correctly to handle both the {ViewToUpdate} string as well as still pass in the querystring?

TIA

2 Answers 2

1

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>

The view codebehind:

  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
Sign up to request clarification or add additional context in comments.

2 Comments

One question. Why is the Webform Page Event there? IE. Page_Load.
From the olden days before MVC.Net got rid of them would be my guess.
0
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) {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.