![]() |
|
[C#] Custom Web API Controller class? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Custom Web API Controller class? (/showthread.php?tid=5595799) |
Custom Web API Controller class? - severos - Apr 9, 2017 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??? * - iNCEPTiON_V - Apr 9, 2017 What are you using Asp.net or Asp.net Core ? * - severos - Apr 9, 2017 (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. * - iNCEPTiON_V - Apr 9, 2017 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! * - severos - Apr 9, 2017 (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. * - Deliriumâ„¢ - Apr 10, 2017 You should use ServiceStack if your project contains up to 10 DTOs and endpoints. It's more flexible than WebAPI 2. * - ShadowCloudGH - Apr 10, 2017 (Apr 9, 2017 09:20 PM)severos Wrote: Currently deployment is to Windows servers. You could just create a new folder called base and drop a [project]baseApiController in there. Code public class BaseController : ApiControllerThat 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()* - severos - Apr 10, 2017 (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. * - severos - Apr 13, 2017 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 Code Any idea what's the problem there??? |