1

I've got a situation where most of the requests will be processed with dedicated controllers.

    config.Routes.MapHttpRoute(
            name: "DataApi",
            routeTemplate: "api/{controller}/{action}",
            defaults: new
            {
                controller = "default"
            }
    );

But there may be a situation where no controller will be found based on URL. Like api/this_controller_is_not_exists/GetStatus.

How can I redirect those requests to default controller called default and process this request using action inside default controller? Valid requests for default controller would look like api/default/GetStatus.

The route will be no much different for valid and invalid request. Valid:

'api/this_controller_exist/GetStatus'

Invalid:

'api/this_controller_not_exists/GetStatus'
4
  • What does the defaultController do? Commented Jul 21, 2017 at 9:47
  • @ChetanRanpariya : It contains defaults actions where 'this_controller_not_exists' can be used as a parameter to further processing with default logic. Commented Jul 21, 2017 at 9:50
  • As a workaround you can handle 404 error and redirect to default controller manually Commented Jul 21, 2017 at 9:54
  • You can handle this by writing custom error handler which will redirect to default controller if passed controller is not found. You can also write custom controller factory which will inject DefaultController if the passed controller is not passed. But I don't understand why you need this feature. You are actually opening up your API for attack where any URL targeting to your API domain will go to default controller and return response. This can lead to DOS attack. Commented Jul 21, 2017 at 10:26

1 Answer 1

1

Well if you have a default route mapping named default and if your controller does really exists then it will automatically map the request to your controller this_controller_exist cause after the requested URI passes through RouteData it will be feeded to HttpControllerSelector which will try to get all the controller in your API project inherits from ApiController using reflection. Thus if the controller in your question does really exists and derives from ApiController then it won't be an issue provided you have a default route mapping in WebApiConfig else if not found it will resort to IIS resource handling which will end up throwing a 404 resource not found error.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you actually pointed me a solution. As you said it should work by default with Default controller I've started to looking why it's not working for me. I found that my default controller class cannot be an abstract class. So instead of abstract DefaultClass : ApiController I am using now DefaultClass : MyBaseClass where MyBaseClass is abstract MyBaseClass : ApiController.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.