Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Custom Web API Controller class?

When creating WebAPI project, all controllers inherit the class ApiController and methods are declared in it.
Is it possible to create a custom controller class that customizes behavior of those controllers? stuff I'm looking for is some pre checks for the request before it's passed to handler (yes I know it's possible to do it using filters), and then formatting the response in a specific way. The combination of pre/post behaviors is what makes things a bit nasty since some pre data is required in the post data, that's why I'd like to use an abstract class rather than a bunch of filters and passing them around like crazy.
The custom class would require the controller to set a 'response object' instead of writing directly to response, and when method returns it'll format it in the appropriate format.

Or did I forget about some easier solution???
Have a cool signature? hit me up and I might use it.
What are you using Asp.net or Asp.net Core ?
(Apr 9, 2017 08:18 AM)iNCEPTiON_V Wrote:  What are you using Asp.net or Asp.net Core ?

Currently running ASP.NET, the lower required version the better it'll be since there are lots of legacy code that it'll be a bit hard to change them. But, if no other possible way, I'll upgrade to Core.
Have a cool signature? hit me up and I might use it.
If you are planning to deploy on a Linux or any other system ill HIGHLY recommend Core, I'm using it as a backend for my mobile app, it makes life so much easier no need for servlets and Java. If you need help with core Im glad to help!
(Apr 9, 2017 12:39 PM)iNCEPTiON_V Wrote:  If you are planning to deploy on a Linux or any other system ill HIGHLY recommend Core, I'm using it as a backend for my mobile app, it makes life so much easier no need for servlets and Java. If you need help with core Im glad to help!

Currently deployment is to Windows servers.
Do you know how to make the custom webAPI controller class? that's all I need for now.
Have a cool signature? hit me up and I might use it.
You should use ServiceStack if your project contains up to 10 DTOs and endpoints. It's more flexible than WebAPI 2.
(Apr 9, 2017 09:20 PM)severos Wrote:  Currently deployment is to Windows servers.
Do you know how to make the custom webAPI controller class? that's all I need for now.

You could just create a new folder called base and drop a [project]baseApiController in there.

Code
public class BaseController : ApiController
    {

    }

That being said, it sounds like you might be solving the problem in the wrong place.

Could you give us an example of the problem you are trying to solve? Your controllers should not have logic in them. They should be calling code that's written against interfaces...

Code
public HttpResponseMessage SomeMethod()
        {
            return Abstraction.MethodName();
        }
(Apr 10, 2017 03:48 AM)Delirium™ Wrote:  You should use ServiceStack if your project contains up to 10 DTOs and endpoints. It's more flexible than WebAPI 2.

It's not a matter of many endpoints in project, it's just that there are lots of systematic steps carried on controllers in tons of projects, so I'm looking to create a small custom library that I'll simply include it to make things easier and faster.
Have a cool signature? hit me up and I might use it.
Update:

I've created a class that inherit from ApiController class, does some initialization by overriding the ExecuteAsync method (initialize then call base method), However now it seems that application can't even see anything in there.
Ex:
PHP Code

<?php 
class testController : ApiController{
  public 
string Test(){ return "";}
} 
This works perfectly and returns the empty string. however...
PHP Code

<?php 
class testController : CustomController{
  public 
string Test(){ return "";}
} 
where CustomController is the class I made that inherits from ApiController. When I try to enter the route for Test method it returns 404 (not found) page...

Any idea what's the problem there???
Have a cool signature? hit me up and I might use it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Too FarJul 14, 2022 09:00 AM 2 159 Jul 14, 2022 09:00 AM
Last Post: Too Far
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[Java] by GlushkoJun 13, 2018 11:13 PM 3 642 Jun 13, 2018 11:13 PM
Last Post: hostov