Milson
Milson

Reputation: 1575

What can be the cause for error while building ASP.NET web application?

While I build my solution it shows following error:

'System.Web.HttpRequest' does not contain a definition for 'RequestContext' and no extension method 'RequestContext' accepting a first argument of type 'System.Web.HttpRequest' could be found (are you missing a using directive or an assembly reference?)

What dll or reference I am missing!

Code:

 public void ProcessRequest(HttpContext context)
    {
        try
        {
            HttpRequest request = context.Request;
            string methodName = "";
            if (request.RequestContext.RouteData.Values["methodName"] != null)
            {
                methodName = request.RequestContext.RouteData.Values["methodName"].ToString();
            }
            else
            {
                methodName = request.RawUrl.Substring(request.RawUrl.LastIndexOf("/") + 1);
            }

            //if form post was made way to get values
            //context.Request.Form["firstName"]; //parameter key
            string json = new System.IO.StreamReader(context.Request.InputStream).ReadToEnd();
            JavaScriptSerializer jss = new JavaScriptSerializer();
            ServiceProcesser process = new ServiceProcesser(typeof(AspxCommerce.AdminNotification.AdminNotificationController), json, methodName);
            var response = process.Invoke();

            if (response != null)
            {
                context.Response.ContentType = "application/json";
                context.Response.Write(jss.Serialize(new { d = response }));
            }
            else
            {
                context.Response.ContentType = "application/json";
                context.Response.Write(jss.Serialize(new { d = "" }));
            }
        }
        catch (Exception ex)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            context.Response.ContentType = "application/json";
            var resp = new { d = ex.Message.ToString() };
            context.Response.Write(jss.Serialize(resp));
        }
    }

For full source you can explore my github repo: https://github.com/Milstein/Ratamata

Upvotes: 1

Views: 2304

Answers (2)

Sampath Dilhan
Sampath Dilhan

Reputation: 815

I had a similar issue. In my case it was due to the wrong assembly that has been referred (yes, a silly mistake..) . Which was *C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Http.dll*.

Then I installed Microsoft.AspNet.WebApi.Core package using Install-Package Microsoft.AspNet.WebApi.Core command.

Then it will refer *~\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll*.

Upvotes: 0

Bobson
Bobson

Reputation: 13706

HttpRequest.RequestContext was not introduced until .NET version 4, as can be seen in the "Version Information" section of the official documentation.

My guess is that you're using .NET 2.0 or 3.5, so you don't have access to that property. You can check this by right-clicking on your website project, choosing Properties from the dropdown, and checking the Target framework option on the Application tab.

Upvotes: 3

Related Questions