I should create a website with multiple sections. The functionalities and views for these sections are exactly the same but I want different URL such as
- //localhost:111/Works/Index
- //localhost:111/OldJobs/Index
For this reason, I created a BaseReferenceController
with all ActionResult
I need. For example:
public class BaseReferenceController : Controller
{
public virtual ActionResult Index(int? sectionId)
{
Articles articles = GetArticles(sectionId);
// more code base on sectionId
return View(articles);
}
// more ActionResult
}
Now I create a new controller to have a new URL Works
like
public class WorksController : BaseReferenceController
{
public override ActionResult Index(int? sectionId)
{
return base.Index(2);
}
}
An error occurs
The view 'Index' or its master was not found or no view engine
supports the searched locations. The following locations were
searched:
~/Views/Works/Index.aspx
~/Views/Works/Index.ascx
~/Views/Works/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Works/Index.cshtml
~/Views/Works/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
My intention was that BaseReferenceController
creates the page and WorksController
returns the ActionResult
comes from BaseReferenceController
.
I tried to use RedirectToActionPermanent
but I can't have the result I want.
Update Forget breadcrumbs.
Update/2
Based on @ironstone13 answer (thanks!) I tried to create a new route:
routes.MapRoute(
"Works",
"Works/{action}/{id}/{slug}",
new { controller = "BaseReferenceController", action = "Index",
sectionId = 2, slug = UrlParameter.Optional },
new { id = @"\d+" });
Where action
is the action
in my Controller
, id
is the identifier of a record to show or edit (if it is necessary), slug
is slug :) but I need a room to pass the sectionId
(because without this I don't know how to filter result).
I received a message in Insight debug
The controller for path '/Works' was not found or does not implement IController.
and in Visual Studio I receive this
System.InvalidOperationException occurred HResult=0x80131509
Message=The constraint entry 'slug' on the route with route template 'Works/{action}/{sectionId}/{slug}' must have a string value or be of a type which implements 'System.Web.Routing.IRouteConstraint'.
Source=System.Web.Mvc
Where am I wrong? After that I can call
- //localohost:111/Works/Index
- //localohost:111/Works/New/2
- //localohost:111/Works/View/23/Document-for-client
? Thanks