2

I want to pass url link like

http://localhost:24873/Jobs/[companyname]
or
http://localhost:24873/[companyname]/Jobs/ (Preferred)

I tried below routing in global aspx file and created controller named Jobs and Index action result with Jobs folder but not working.

 routes.MapRoute(
             "JobList", // Route name
             "Jobs/{companyname}",
              new
              {
                  controller = "Jobs",
                  action = "Index",
                  companyname = string.Empty
              }
          );

And my controller:

public partial class JobsController : Controller 
{ 
    public ActionResult Index() 
    { 
          JobsListModel model = new JobsListModel(); 
          return View(model); 
    } 
}

What I am doing wrong? Please help.

6
  • 1
    What do you mean by not working? It's not executing the controller method? It's not matching the route? It's giving an error message? Have you tried Phil Haack's route tester on it? Commented Mar 14, 2012 at 16:43
  • it is giving resource cannot be found page. Can you please correct me for routing? to match and work like my example url Commented Mar 14, 2012 at 16:45
  • 2
    Did you add this route as the first MapRoute entry in Global.asax file? Commented Mar 14, 2012 at 16:50
  • great.. I added route as the first MapRoute entry in Global.asax file and its working now... Can you please tell me reason for it that why I need to make that as first map route entry? Commented Mar 14, 2012 at 16:53
  • @Keyur: Apparently it's matching one of your other routes. The other route it is matching is apparently broken (it doesn't have a corresponding controller method). Commented Mar 14, 2012 at 16:55

4 Answers 4

4

To add to the previous answer, if you want your preferred route of http://localhost:24873/[companyname]/Jobs/ to work, add this route, again before your default route.

routes.MapRoute(
  "JobList", // Route name
  "{companyname}/Jobs",
  new
  {
     controller = "Jobs",
     action = "Index",
     companyname = string.Empty
  }
);
Sign up to request clarification or add additional context in comments.

2 Comments

how/where is the company name being added to the url?
it would be passed through the url when the page was requested, that's what the {companyname} in the route definition suggests, the string.Empty, simply gives a default value
3

You must add this route as the first entry in global.asax, otherwise the request will be routed to the default route (or route before it)

Source

2 Comments

Thanks but still getting little confused about its ordering. As I am also adding it after default root. Do you mean that adding it as first to check custom routes first.
@Keyur - Yes, you should add this custom route first, and add the default route after. The request will first try to use your custom route.
0

Your jobs controller needs an index action with a named parameter of companyname like such:

public ActionResult Index(string companyname) 
{ 
      //Do some checking on the name
      JobsListModel model = new JobsListModel(); 
      return View(model); 
}

You will probably still need the empty action as well:

public ActionResult Index() 
{ 
    return Index(string.Empty);
}

Oh, missed this. Like the guy ahead of me said to get your other url to work do this: (http://localhost:24873/[companyname]/Jobs/)

routes.MapRoute(
  "JobList", // Route name
  "{companyname}/Jobs",
  new
  {
     controller = "Jobs",
     action = "Index",
     companyname = string.Empty
  }
);

Comments

-1

Try this:-

//Default url

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "Default",
    "", 
    new { controller = "Home", action = "Index", id = "" }
);

//others url rewriting you want

RouteTable.Routes.MapRoute(null, "Jobs/{companyname}", new { controller = "Jobs", action = "Index" });

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.