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.
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
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
}
);
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
}
);
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" });