ASP.NET Web API Help Pages Omit Controllers That Inherit From A Base Controller - asp.net-web-api

I have a controller that I want to generate documentation for using ASP.NET Web API Help Pages.
When I directly inherit from ApiController the documentation appears:
public class ExampleController : ApiController
But when I inherit from a base controller, it is omitted:
public class ExampleController : ApiBaseController
...
public class ApiBaseController: ApiController
I have switched to delegation rather than inheritance, but I wanted to know how to make it work with inheritance.

Here is a tip I picked up in my experimentation.
The documentation leans heavily on the routes in your API config. If your controller isn't covered by a route, it won't show up. Additionally, the order of the routes in your API config is the order of the operations in your documentation.
To cover both of these points I have created named routes for each controller. This has the added benefit of making each route specific, rather than a single route with lots of optional bits. This ensures all my operations appear in the documentation, in a good order.
I have also added the API tester so the API can be called directly from the documentation.

Check the permissions in your base class. I had the same issue and is was a result of methods that should have been set as internal being protected.
Make sure that all your methods that need to be accessed by the parent item are set to internal and any methods that override the ApiController are set to protected.
Post your code if it still doesn't work.
Works like Gravy :)

Related

Securing and permitting access to spring rest controller with ant matcher and method level security side-by-side?

First of all my application is build with spring boot and security.
So I have several rest controllers (resources). One controller provides multiple methods to get/post different kind of data. But I have cases where some methods should be public and others needs authentication.
For example:
GET /api/object/method1 <-- Needs authentication
GET /api/object/method2 <-- Public
POST /api/object/method3 <-- Needs authentication
POST /api/object/method4 <-- Public
What is best practice to secure this resource? I can't secure url with antMatcher with following pattern /api/object/**. Because then the public methods would be secured as well. Also I can't secure by request type (GET, POST).
One option I thought about was using only method level security (eg #Secured etc). This would mean that I need to annotate a lot of methods.
Another thought that comes to mind is dividing resource to 2 parts.
For example creating
ObjectResource.java
ObjectResourcePublic.java
One controller base URL would be /api/public/ and second simply /api/
Then I could use antMatcher for these URLS.
Is my only option to secure every path separtely or every method separetly?
What other options do I have to do this kind of partial securing one resource?
You may use below methods apart from above mentioned methods.
1. Write Interceptor/filter
2. Use Aspect and define advise

How to layout base class and 3 extended classes in CodeIgniter?

This is the layout I'd like to use for controllers in CodeIgniter:
(base) editor
(extend) design
(extend) content
(extend) ...php
...where editor is a base class and design, content, etc. controllers extend editor. The editor class will have methods that are publicly accessible from the extended classes URL segments.
I've read some topics on here, and they recommended:
Library - this won't work as methods won't be publicly accessible (am I correct?)
Put base class in the same file as extended class and name the controller that - this won't work since I need to extend from multiple places.
Put all the files in the controllers folder, add require statements to each extended class - is this bad form?
I'm new to CI. What's the proper/correct way to handle this?
Thanks!
http://www.ellislab.com/codeigniter/user-guide/general/core_classes.html
extend the core CI controller with MY_Controller, then extend MY_Controller with your other controllers. all the other retain the functionality in MY and MY retains the functionality of the base controller
There is also this article which allows more controllers that don't have the MY_ prefix, which I use and find VERY useful!
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY

Regarding Symfony2 Session

In symfony2 every user created controller extends Controller Class as shown below,
class MyController extends Controller {
thus functions related to session handling are available with $this object, But controllers in Vendor and Core don't extend Controller class thus don't provide access to session related functions. So is there any way to use these functions without extending Controller class.
Presently I am using $_SESSION[], for setting and getting session variables.
Is there any way other than above.
Symfony2 provides a service for sessions, this is what you're trying to retrieve. All services in symfony2 are retrieved using the service container, which is what you're referring to with
$this->get('session');
To properly make use of the service container in your own controllers you can either...
Configure your controllers as services (see: here)
Extend the base Controller class provided by the Symfony2 stack (making the get() method available to your child Controller)
The first option is the correct way to go, you have full control over what services are then injected into your respective controllers (see service container documentation)

Codeigniter - Hooks per one controller

I am using hooks in my CI application "pre_controller hook specifically".
But the problem is Hooks are activated each time a request is issued to any other controller even controllers that i don't want the hook to be activated in.
Can hooks be enabled for only one controller? just like the #Before annotations in playframework.
Thanks in advance.
Why don't you put that logic in the Constructor of your controller?
If you have multiple controllers you wish to share this functionality, simply extend the CI_Controller with a new class in application/core/MY_Controller.php and put the functionality in there, then in the controllers have them extend that class instead of the default CI_Controller (you can add more than one class in MY_Controller.php)!
Anything that you put in the override class in MY_Controller.php would execute before the code in the rest of the controller, simulating the pre_controller hook.
Just remember to call the parent constructor as well:
function __construct(){
parent::__construct();
}
See the manual for more info about extending the core: https://www.codeigniter.com/user_guide/general/core_classes.html
You could also put your code into a library to use whenever you need it. I ended up using my solution because I could keep my authentication logic separate to my modules. It makes it easier for updating as well.

Inject cookies into controller

Is there any way to inject the cookie dependecy to a controller? Or do i have to write my own interface and wrapper class around the Cookie collection class?
I think you're asking about whether you can get a Cookie as a parameter to an Action. I don't believe you can do this, so you'll have to hit the Cookie class directly.
What we do in this case (when cookie based data is required by most of the actions in an application) is put a utility method in a Controller base class and then have all our controllers descend from that. Makes it very easy to use the Cookie in an Action, and centralizes the code for extracting it.
Since no better answered surfaced I just implemented a interface and injected that for concrete scenario and Mocked it for test

Resources