0

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.

Arman
  • 5,136
  • 3
  • 34
  • 36

3 Answers3

1

You scenario should just work with attribute routing without having to changing the name to PswdrecController. Are you sure you are using the Route attribute from System.Web.Http namespace?

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Yea, I'm using that one but it has no effect at all. I'm thinking could it be related with this post? http://stackoverflow.com/questions/17318901/mvc-4-5-web-api-routing-not-working/17321724#17321724 – Arman Mar 24 '14 at 17:41
  • I unable to repro your issue at my end. Could you share how your route configuration looks like? – Kiran Mar 24 '14 at 17:44
  • Updated the question, so you can see if there's anything suspicious. looks as a regular WebApi config stuff. – Arman Mar 24 '14 at 17:52
1

To configure the router open the file App_Start/WebApiConfig.cs and put this code below:

           config.Routes.MapHttpRoute(
                name: "MyCustomRouteApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { controller = "mobile", action = "pswdrec", id = RouteParameter.Optional}                
            );

Edit the line of defaults: to your case. It's just a example.

Hope help you!

Daniel Melo
  • 548
  • 5
  • 12
  • And then putting the `[ActionName("pswdrec")]` on the Post verb you mean? – Arman Mar 25 '14 at 10:41
  • 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. – Arman Mar 25 '14 at 11:13
1

For Attribute routing, do you have routes.MapMvcAttributeRoutes(); in your RegisterRoutes? I don't see it in your code. Attribute routing will not work if this line is not there.

mugdhak
  • 31
  • 3