1

So I am currently implementing security on a project I am working on and I followed the guide for identityServer3 to add it to my mvc5 application. I got through the complete setup and thought everything was good, until I realized that routes in my api, unless they were the very basic ones, /api/.../ no longer work.

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

I am using the default routing, and on the various pieces of my api controllers I have put route attributes to guide them in the event they fall outside of this format. for example:

    [Route("api/Location/GetByAreaIncludeFileStore/{id}")]
    public IEnumerable<Location> GetLocationsByAreaIdIncludeFileStore(int id)
    {
        if (id <= 0)
        {
            return null;
        }
        IEnumerable<Location> locations = _lookupService.GetLocationsByAreaIdIncludesFileStore(id);
        return locations;
    }

and as i said earlier, prior to adding identity server theses worked beautifully. During the addition of IdentityServer I had to add a few nuget packages to my webapi:

    install-package Microsoft.Owin.Host.SystemWeb
    install-package Microsoft.Aspnet.WebApi.Owin
    install-package Thinktecture.IdentityServer3.AccessTokenValidation

So basically my question after all is said and done is, How can I fix my routes so I can get all of the information I need?

Currently I have routes that are

    api/controller
    api/controller/id
    api/controller/action
    api/controller/action/id

Any Help would be amazing, Thanks! Also, I looked through many of the other posts and tried a lot of variations of routing and attributes before asking this question.

Dave
  • 11
  • 3
  • For example if I make the api call /api/area/2 The api should return area 2's information but now I get the response: "ExceptionMessage": "Multiple actions were found that match the request: \r\nDefaultAction on type XXXXXX.Api.Controllers.AreaController\r\nGetWithFileStores on type XXXXXXX.Api.Controllers.AreaController\r\nGetAreasByTourId on type XXXXXXX.Api.Controllers.AreaController", so the routes tags that have the actions aren't being recognized – Dave Aug 05 '15 at 13:36
  • Sorry I see ti now.. – BillRuhl Aug 05 '15 at 13:39
  • I believe that you need to define an action on your default route...config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { controller = "myController", action = "MyAction", id = RouteParameter.Optional } ); – BillRuhl Aug 05 '15 at 13:51
  • If I make that change it results in webapi, trying to use(using the example /api/area/2 ) 2 as an action and the response is exactly that: "Message": "No HTTP resource was found that matches the request URI 'https://localhost:44302/api/Area/2'.", "MessageDetail": "No action was found on the controller 'Area' that matches the name '2'." – Dave Aug 05 '15 at 13:56
  • Decorate the Index method in the area controller with [Route("")] that should route a call with out an action to the index method. – BillRuhl Aug 05 '15 at 14:17

2 Answers2

1

Add this line in your WebApiConfig config.MapHttpAttributeRoutes(); before your config.Routes.MapHttpRoute().

dan
  • 522
  • 6
  • 15
  • I had that already, I just posted the routing snippet, sorry if that portion wasn't clear. The solution that I posted is what fixed it. – Dave Aug 13 '15 at 13:51
0

I was actually able to get it working using the method described in the solution in this post: MVC 4.5 Web API Routing not working? I tried doing this yesterday, and it didn't seem correct, but with a little more spit and polish I ended up achieving proper routes. They recommended having the route config as follows:

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

and then following that pattern change all the basic routes like:

[ActionName("DefaultAction")
public string Get()
{
}

[ActionName("SpaceTypes")]
public string GetSpaceTypes(int id)
{
}

It worked, I had to refactor all of my api calls and my restful services to match, but nonetheless, I am back to functioning.

Community
  • 1
  • 1
Dave
  • 11
  • 3