0

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;
 }
}

}

4
  • 1
    So what type do you expect Request to be? My guess is that you're actually trying to call this method but you're missing the fact that in the context you originally saw the code in, there was a Request property... Commented Dec 8, 2015 at 20:58
  • 1
    It's not at all clear what this code is trying to do, in fact... why are you trying to create a response if you're just a console app that isn't serving requests? Commented Dec 8, 2015 at 21:00
  • 1
    It looks like your project is a Console application. The Request property is accessible from web project types. Commented Dec 8, 2015 at 21:02
  • Thanks everyone for your input, I am missing the previous context and have opted for another route to get what I need. Commented Dec 9, 2015 at 22:07

2 Answers 2

2

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

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

Comments

1

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

Comments

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.