1

I'm trying to write a method to handle Facebook webhooks challenge as so:

using System.Web;
using System.Web.Http;    
[HttpGet]    
    public HttpResponseMessage Get()
            {
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(HttpContext.Current.Request.QueryString["hub.challenge"])
                };
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
                return response;
            }

When building the project, I receive an error which states that "Http" does not exists in the namespace "System.Web" (and with it "HttpGet"). I tried reinstalling Microsoft.AspNet.WebApi through NuGet console but saw no change. Also tried adding this to the packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.8" targetFramework="net45" />
  <package id="Microsoft.Net.Compilers" version="2.6.0" targetFramework="net45" developmentDependency="true" />
  <package id="MSBuildTasks" version="1.5.0.235" targetFramework="net452" developmentDependency="true" />
  <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
</packages>

web.config file:

    <?xml version="1.0"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5.2" />
      </system.Web>
  -->
  <system.web>
    <compilation targetFramework="4.5"/>
  </system.web>
</configuration>

Thanks for your help :)

Quit3Simpl3
  • 183
  • 2
  • 14
  • Nope. It's not the same error, and already tried adding the refernces again. – Quit3Simpl3 Dec 11 '17 at 12:08
  • Would you share web.config file too ? – lucky Dec 11 '17 at 12:43
  • Added it to the post – Quit3Simpl3 Dec 11 '17 at 12:53
  • 1
    Reinstalling Web API won't help you if you actually created an ancient ASMX service - those predate WCF (think 2008) and most of the SOAP interoperability standards. They definitely don't support ASP.NET Web API. *What* kind of application did you create and how? Which Visual Studio version did you use? Simply creating an ASP.NET Web API project will work. If it didn't thousands of developers would have noticed already. – Panagiotis Kanavos Dec 11 '17 at 12:57
  • 1
    In a Web API project you don't need to use either the `HttpGet` attribute nor name your method `Get`. Adding the Web API package to an ASXM or even a WCF project won't work. – Panagiotis Kanavos Dec 11 '17 at 12:59
  • You should probably start with an ASP.NET Web API tutorial like [Getting Started with ASP.NET Web API 2 (C#)](https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api). – Panagiotis Kanavos Dec 11 '17 at 13:02
  • It's a pretty big project. Unfortunately I can't create a new one. Could you please elaborate on the way to this with ASMX? – Quit3Simpl3 Dec 11 '17 at 13:07
  • google how to return a response from an ASMX service. There'll be plenty of worked examples and tutorials if you search for the correct technology framework. If it's a big project though, are there not some other examples already in the code which already do this kind of thing? Or are there no other web services in there yet? Plus, a _solution_ can consist of multiple _projects_ quite happily - a UI (or three), an API, a background service, a database project, some class libraries etc etc. I don't really see why you can't add a Web API into your solution if it's useful. It's better than ASMX – ADyson Dec 11 '17 at 13:15
  • But would it work if the solution is a few years old? – Quit3Simpl3 Dec 11 '17 at 13:22
  • @parkingavenue, an asmx is legacy webservice not the modern web api 1 and 2 (different implementation and classes involved). Webservices are SOAP, typically you send an XML document (the envelope) by POST and retrieve an XML response. Not sure why you need to use GET but I'm wondering how to send an XML document in the url by GET, perhaps encoding the whole damn thing? Check out this answer https://stackoverflow.com/a/618968/2516718 – derloopkat Dec 11 '17 at 13:28
  • @parkingavenue how do you define "a few"? Looks from your config like you're targeting .net 4.5 so yeah Web API is fine with that framework version, it's pretty recent anyway. Different projects within a solution can target different frameworks though, if you need them to. Give it a try. – ADyson Dec 11 '17 at 17:03

0 Answers0