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.
Add this line in your WebApiConfig config.MapHttpAttributeRoutes();
before your config.Routes.MapHttpRoute()
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.