aqpham1
aqpham1

Reputation: 31

Reference using System.Web.Http is not working? (using .net 4.5)

After searching around here for over two hours, I still can't get the code I pieced together from here to work due to a missing reference I believe.

The error I am getting is from the part below in which Request does not exist in the current context.

return Request.CreateResponse(HttpStatusCode.BadRequest);

Another error I am getting is that getFileFromID also does not exist in the current context.

getFileFromID(id, out fileName, out fileSize);

I'm sure it's just a simple reference I am missing but I've tried googling and still can't find the solution. Does anyone know why I keep getting the "does not exist in the current context" error?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Morningstar.JSON;
using Ionic.Zip;
using System.Net;
using System.IO.Compression;
using System.Windows.Forms;
using Ionic.Zip;
using System.Web;
using System.Net.Http;
using System.Web.Http;


namespace MS_Hourly_API_Call
{
class Program
{

 public static void Main(string[] args)
    {
        // THIS ONE IS DIFFERENT FROM THE ORIGINAL DOWNLOADCURVEDATA
        string feedName = "RiskReporting_Power_Hub_Hourly";
        var url = String.Format("https://mp.morningstarcommodity.com/lds/lists/{0}/content?fromDateTime={1}", feedName, DateTime.Today.ToString("yyyy-MM-dd")); 
        string username = "asdfk"; //removed username
        string password = "asdfasdf"; //removed password

        // Setup web connection with appropriate authentication parameters and requesting a JSON response
        using (var syncClient = new WebClient())
        {


            syncClient.Headers.Add("Accept", "application/json");
            syncClient.Credentials = new NetworkCredential(username, password);

            // Retrieve and parse the JSON response
            var jsonContent = syncClient.DownloadString(url);
            var feedItems = JsonConvert.DeserializeObject<List<FeedContent>>(jsonContent);

            // Download each item
            syncClient.Headers.Remove("Accept");




        }
    }
 public HttpResponseMessage GetFile(string id)
 {
     if (String.IsNullOrEmpty(id))

         return Request.CreateResponse(HttpStatusCode.BadRequest);

     string fileName;
     string localFilePath;
     int fileSize;

     localFilePath = getFileFromID(id, out fileName, out fileSize);

     HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
     response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
     response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
     response.Content.Headers.ContentDisposition.FileName = fileName;

     return response;
 }
}

}

Upvotes: 0

Views: 968

Answers (2)

hawkstrider
hawkstrider

Reputation: 4341

If this is an application calling the web to get a file, then you can use a WebClient to get the file https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx

It has built in methods for downloading files synchronously or asynchronously

Upvotes: 1

Avi
Avi

Reputation: 1964

As far as I know there is no Request object without IIS and your code clearly doesn't run on IIS server

Upvotes: 2

Related Questions