How to modify MapRoute (add new dir)
How can I change my map route so I can have another dir, and not just the controllers and actions.
What I want is to have another directory before the controller so that way I can separate controller per "module"
routes.MapRoute(
"Default", // Route name
"{module}/{controller}/{action}/{id}", // URL with params
new { module = "module", controller = "controller", action = "Index", id = ""}
This is what I want, but the cod开发者_运维知识库e doesn't work. What more do I need to change to change the MapRoute?
Have you considered looking at the Areas feature of ASP.NET MVC 2? It can address the requirements you have of partitioning a site into distinct "modules".
Walkthrough: Organizing an ASP.NET MVC Application using areas
I don't have a better solution but would try to implement my own ViewEngine.
If your controllers are in modules so that doesn't much standard ASP.NET MVC convention and routes cannot be resolved.
I think that you should implement your own ViewEngine to support it.
For example :
public class ModuleViewEngine: WebFormViewEngine
{
public ModuleViewEngine()
{
ViewLocationFormats = new[]
{
"~/views/{2}/{1}/{0}.aspx",
"~/views/{2}/{1}/{0}.ascx",
"~/views/Shared/{1}/{0}.aspx",
"~/views/Shared/{1}/{0}.ascx",
};
MasterLocationFormats = new[]
{
"~/views/{1}/{0}.master",
"~/views/Shared/{0}.master",
"~/views/{2}/{1}/{0}.master",
"~/views/{2}/Shared/{0}.master",
};
}
public override ViewEngineResult FindPartialView(ControllerContext controllerContext,
string partialViewName, bool useCache)
{
//coede
}
public override ViewEngineResult FindView(
ControllerContext controllerContext,
string viewName, string masterName, bool useCache)
{
//code
}
}
we redefine the ViewLocationFormats and MasterLocationFormats arrays to accommodate an extra parameter (area). The view locations use standard string formatting placeholders. The first placeholder, denotes the action name. The second placeholder,is the controller name and the final placeholder, is the module name (if specified).
FindPartialView and FindView methods, so we must override these as well.
Because our controllers will now reside in a subfolder of the Controllers folder, we have to register the additional controller namespaces:
ControllerBuilder.Current.DefaultNamespaces.Add("Module.Controllers.something");
This is required because the default ControllerBuilder does not want to reflect over the entire assembly looking for the controller when matching a route to a specific controller.
精彩评论