asp.net mvc - WebAPI Controller Names with Underscores -
webapi has naming convention "foocontroller" controller names. similar asp.net mvc. our codebase uses underscores separate words in identifier named, e.g. "foo_bar_object". in order controller names follow convention, need way name our controllers "foo_controller".
basically, don't want url routes "oursite.com/api/foo_/", , don't want have make exceptions everywhere underscore (e.g. route config, names of view folders, etc). in mvc, do:
global.asax
protected void application_start() { ... controllerbuilder.current.setcontrollerfactory(new custom_controller_factory()); }
custom_controller_factory.cs
public class custom_controller_factory : defaultcontrollerfactory { protected override type getcontrollertype(requestcontext request_context, string controller_name) { return base.getcontrollertype(request_context, controller_name + "_"); } }
this seems take care of problem in once place in mvc. there way same webapi? i've heard rumor of defaulthttpcontrollerfactory can't find anywhere.
i think defaulthttpcontrollerselector looking for.
class customhttpcontrollerselector : defaulthttpcontrollerselector { public customhttpcontrollerselector(httpconfiguration configuration) : base(configuration) { } public override string getcontrollername(httprequestmessage request) { ihttproutedata routedata = request.getroutedata(); string controller = (string)routedata.values["controller"] return controller + "_"; }
global.asax
globalconfiguration.configuration.services.replace(typeof(ihttpcontrollerselector), new sh_custom_http_controller_selector(globalconfiguration.configuration));
Comments
Post a Comment