8

I'm getting this error on this method:

        [HttpGet]            
        public HttpResponseMessage Search([ModelBinder(typeof(ApiDataTablesModelBinder))] IDataTablesRequest requestModel)
        {
            var sortedColumn = requestModel.Columns.GetSortedColumns().SingleOrDefault();

            bool isDesc = false;
            string sortField = "ID";

            // get all projects and order them
            var projects = lkp_surveyRepo.GetAll();//.OrderBy(sortField, isDesc).ToList();

            // filtered projects
            var filteredResults = projects;

            filteredResults = projects.Where(x => x.code == selectedDistrictID);

            var pagedResults = filteredResults.Skip(requestModel.Start).Take(requestModel.Length);
            var result = Request.CreateResponse(HttpStatusCode.OK, new DataTablesResponse(requestModel.Draw, pagedResults, filteredResults.Count(), projects.Count()));

            return result;
        }

my controller is implementing Controller:

public class HomeController : Controller
    {

I found this question, but the answer didn't help me:

CreateResponse method in asp.net Web API

complete error is:

Error 13 'System.Web.HttpRequestBase' does not contain a definition for 'CreateResponse' and the best extension method overload 'System.Net.Http.HttpRequestMessageExtensions.CreateResponse(System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode, T)' has some invalid arguments

do you see what I'm doing wrong? maybe something wrong with whats inside the CreateResponse method... thanks

1

1 Answer 1

26

I have a feeling you're inheriting the wrong base controller type. If you're using WebAPI, you should inherit from ApiController, not Controller.

In ApiController, Request is System.Net.Http.HttpRequestMessage. In Controller, Request is System.Web.HttpRequestBase. So the extension method is close, but the incorrect types which is giving you the signature method mismatch.

4
  • I'm not using WebAPI. The controller was using : Controller before, I just added a new method below that needed to return the HTTPResponse, so that is new to the class.
    – noobieDev
    Jun 18, 2014 at 14:54
  • @user3461011 CreateResponse and HttpResponseMessage is supposed to be used from WebAPI. In your case, I'd either create a new WebAPI controller so you can use those members, or remove them and return a ViewResult of Json or similar so you can act like a API.
    – Steven V
    Jun 18, 2014 at 15:06
  • You are my hero.
    – Jordec
    Jun 15, 2017 at 18:19
  • how to use HttpRequestMessage in asp.net core
    – P Rane
    Nov 22, 2018 at 17:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.