views:

495

answers:

1

Hi guys,

I am currently creating a mobile version of my company's site and using it as a time to learn asp.net mvc.

The URLs which I'm meant to be creating are as follows

Jobs/in/location
Jobs/for/jobTitle
Jobs/in/location/for/jobtitle
jobs/for/jobtitle/in/location

Now the more types we add the more I'll need to add and different routes I'll need, so not really maintaining.

So currently in my Global.asax.cs is

 
//Jobs
            routes.MapRoute(
                "DefaultJobs",                                              // Route name
                "Jobs/{pagenumber}",                           // URL with parameters
                new { controller = "Jobs", action = "Default", pagenumber = "1" }  // Parameter defaults
            );

            //Jobs by Location

            routes.MapRoute(
                "JobsByLocation",                                              // Route name
                "Jobs/in/{location}/{pagenumber}",                           // URL with parameters
                new { controller = "Jobs", action = "Default", location = "", pagenumber = "1" }  // Parameter defaults
            );

            //Jobs by Title

            routes.MapRoute(
                "JobsByTitle",                                              // Route name
                "Jobs/for/{title}/{pagenumber}",                           // URL with parameters
                new { controller = "Jobs", action = "Default", title= "", pagenumber = "1" }  // Parameter defaults
            );

            //Jobs by Title and Location

            routes.MapRoute(
                "JobsByTitleAndLocation",                                              // Route name
                "Jobs/for/{title}/in/{location}/{pagenumber}",                           // URL with parameters
                new { controller = "Jobs", action = "Default", location = "", title = "", pagenumber = "1" }  // Parameter defaults
            );

So I'm not sure if this is right, since in my JobsController I did have "in" as a Controller, but I could only get one parameter. So I went with a default one which caught everything.


public ActionResult Default(string location, string title, string pagenumber)
        {
            return Content("Location " + location + " Title " + title + " Page " + pagenumber);
        }

Now is this the right way of doing it? Or have I missed something?

For calling this method, I have this


[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchContinent(string Continents)
        {
            try
            {
                // TODO: Add update logic here

                //return RedirectToAction("Default","Jobs",
                    //new {
                       // location = Continents
                        // }); 

                return RedirectToRoute(new { controller = "Jobs", action = "Default", location = Continents, pagenumber = "1" });


                //return Redirect("Jobs/in/" + Continents);
            }
            catch
            {
                return View();
            }
        }

Now this doesn't produce the correct url since it is missing the "in" and passes "location" as a param, which works but is incorrect.

+2  A: 

With RedirectToRoute you should pass the route name you're targetting, so:

return RedirectToRoute("JobsByLocation", new { location = Continents, pagenumber = "1" });
Simon Steele
Cheers, is the rest of the code ok?
Sarkie
I would change pagenumber to an int, and remove the try/catch in SearchContinent - I'm not sure what purpose it serves there. But it's difficult to say without the rest of the context.
Simon Steele