Hi.
I realize that is possible to change the controllers sufix from "Controller".
I read also that changing is not advised (even that i can really understand why?).
Now, how can this be done, and my interest is more for curiosity).
I follow some articles but i get some way lost.
Should this be done on mvc source code or can be done on my project?
If so, a class that inherits from DefaultControllerFactory could be added to folder of controllers of my project?
This has been my way but been i'm lost.
Following this
link i'll had my class somehow like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Dasha.Controllers
{
public class Factory : DefaultControllerFactory
{
protected override Type GetControllerType(string controllerName)
{
String defaultNamespace = "Dasha.Controllers";
String suffix = "mController";
//Prepare the type name
String typeName = String.Format("{0}.{1}.{2}", defaultNamespace, controllerName, suffix);
// Build and return a Type object.
return Type.GetType(typeName);
}
public override void ReleaseController(IController controller)
{
}
}
}
I can overrid getcontrollertype, because it's defined as internal on DefaultControllerFactory. Can someone guide me on this? Thanks.
You should be able to override GetControllerType because it's protected virtual.
However, if you replace the controller lookup logic with your own you will lose the built in caching and performance. If you have a small set of controller names that you need to match to types then it's fine, but if you want to build a general purpose system
then it might become a problem
protectedoverride Type GetControllerType(string controllerName)
You are missing matching signature
protected override Type GetControllerType(RequestContext requestContext,string controllerName)
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
And is it possible to have two suffix to controllers? (Or am I loosing time with this?)
Here is a rough and quick implementation, which calls the base implementation if your suffix controller not found
public class Factory : DefaultControllerFactory
{
protected override Type GetControllerType(RequestContext requestContext, string controllerName)
{
var fullName=controllerName+"MyCustomPrefix";
if (MvcApplication.Controllers.ContainsKey(fullName))
return MvcApplication.Controllers[fullName];
return base.GetControllerType(requestContext,controllerName);
}
public override void ReleaseController(IController controller)
{
base.ReleaseController(controller);
}
}
However, if you replace the controller lookup logic with your own you will lose the built in caching and performance. If you have a small set of controller names that you need to match to types then it's fine, but if you want to build a general purpose system
then it might become a problem
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
dr.Xis
Member
79 Points
83 Posts
change controller sufix
Dec 16, 2010 02:15 PM|LINK
Hi.
I realize that is possible to change the controllers sufix from "Controller".
I read also that changing is not advised (even that i can really understand why?).
Now, how can this be done, and my interest is more for curiosity).
I follow some articles but i get some way lost.
Should this be done on mvc source code or can be done on my project?
If so, a class that inherits from DefaultControllerFactory could be added to folder of controllers of my project?
This has been my way but been i'm lost.
Following this link i'll had my class somehow like this:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Dasha.Controllers { public class Factory : DefaultControllerFactory { protected override Type GetControllerType(string controllerName) { String defaultNamespace = "Dasha.Controllers"; String suffix = "mController"; //Prepare the type name String typeName = String.Format("{0}.{1}.{2}", defaultNamespace, controllerName, suffix); // Build and return a Type object. return Type.GetType(typeName); } public override void ReleaseController(IController controller) { } } }I can overrid getcontrollertype, because it's defined as internal on DefaultControllerFactory. Can someone guide me on this? Thanks.
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: change controller sufix
Dec 16, 2010 06:28 PM|LINK
You should be able to override GetControllerType because it's protected virtual.
However, if you replace the controller lookup logic with your own you will lose the built in caching and performance. If you have a small set of controller names that you need to match to types then it's fine, but if you want to build a general purpose system then it might become a problem
ASP.NET Team
@marcind
Blog
dr.Xis
Member
79 Points
83 Posts
Re: change controller sufix
Dec 17, 2010 10:01 AM|LINK
Yep... I can't override. I got this message:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Error 1 'Dasha.Controllers.Factory.GetControllerType(string)': no suitable method found to override C:\Documents and Settings\ec-outsourcing3\My Documents\Visual Studio 2010\Projects\Dasha\Dasha\Controllers\Factory.cs 11 33 Dasha</div> <div></div>"
Error 1 'Dasha.Controllers.Factory.GetControllerType(string)': no suitable method found to override "
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: change controller sufix
Dec 17, 2010 12:48 PM|LINK
You are missing matching signature
protected override Type GetControllerType(RequestContext requestContext, string controllerName)
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: change controller sufix
Dec 17, 2010 01:00 PM|LINK
Also note that remove the dot before 2,
String typeName = String.Format("{0}.{1}{2}",
defaultNamespace, controllerName, suffix);
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
dr.Xis
Member
79 Points
83 Posts
Re: change controller sufix
Dec 17, 2010 03:28 PM|LINK
an now?
I got this working (thanks mate)
"
protected override Type GetControllerType(RequestContext requestContext, string controllerName) { String defaultNamespace = "Dasha.Controllers"; String suffix = "mTeste"; //Prepare the type name String typeName = String.Format("{0}.{1}{2}", defaultNamespace, controllerName, suffix); // Build and return a Type object. return Type.GetType(typeName); }"
And now? how to use this new sufix to my controls? still don't understand if this can be done in my project or if i had to change mvc2 source code?
And is it possible to have two suffix to controllers? (Or am I loosing time with this?)
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: change controller sufix
Dec 17, 2010 05:06 PM|LINK
you need to add this in Application_Start.
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory())
Here is a rough and quick implementation, which calls the base implementation if your suffix controller not found
public class Factory : DefaultControllerFactory
{
protected override Type GetControllerType(RequestContext requestContext, string controllerName)
{
var fullName=controllerName+"MyCustomPrefix";
if (MvcApplication.Controllers.ContainsKey(fullName))
return MvcApplication.Controllers[fullName];
return base.GetControllerType(requestContext,controllerName);
}
public override void ReleaseController(IController controller)
{
base.ReleaseController(controller);
}
}
global.asax
public static Dictionary<string,Type> Controllers;
protected void Application_Start()
{
Controllers = Assembly.GetExecutingAssembly().GetExportedTypes().Where(t => typeof(ControllerBase).IsAssignableFrom(t) && t.IsPublic && t.Name.EndsWith("MyCustomPrefix", StringComparison.OrdinalIgnoreCase) && !t.IsAbstract).Select(t => t).ToDictionary(t => t.Name);
ControllerBuilder.Current.SetControllerFactory(new Factory());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
Don't forget the expert suggestion,
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD