Have a really basic thing to do - I need to expose a post API such as mobile/api/pswdrec which I wanna bind to my PasswordController to avoid that dull name.
It's a webapi with areas and I'm trying both Attribute Routing and MapHttpRoute
. neither works so far.
public class PasswordController : MobileApiControllerBase
{
//[Route("api/mobile/pswdrec/")]
public RestResponseMessage Post(UserCredentialsModel credentials)
{
return RestResponseMessage.OK();
}
}
Checked the client side and the requests are proper json requests. It's starting to work when I simply change the name of the controller to PswdrecController
.
Is there any other way to "rename" the controller?
working on MVC for years but now stuck on a very simple issue :) thanks for helping out ;)
EDIT:
public class MobileApiAreaRegistration : AreaRegistration
{
private static IWindsorContainer _container = new WindsorContainer();
public override string AreaName
{
get
{
return "MobileApi";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapHttpRoute(
name: "MobileApiDefault",
routeTemplate: "api/mobile/{controller}/{id}",
defaults: new { controller = "Expenses", id = RouteParameter.Optional }
);
}
public static void SetupWebApi(HttpConfiguration configuration)
{/* just javascript serizlizer setup */}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
MobileApiAreaRegistration.SetupWebApi(config);
}
public static void SetupWindsorContainer(IWindsorContainer container)
{
MobileApiAreaRegistration.SetupWindsorContainer(container);
WebApiAreaRegistration.SetupWindsorContainer(container);
}
}
public class WebApiApplication : HttpApplication
{
private static readonly IWindsorContainer _container = new WindsorContainer();
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
InitializeWindsorContainer();
//InitializeExceptionFilters();
}
}
Nothing special but I can't make it work. When I use the original names of the controllers it's shinning like a star :)
Solution: Well, the problem was in the precedence of the routers, so the config with the "api/mobile/pswdrec/{id}" should've been placed BEFORE the default router. Still can't get it working with the attribute routing.