3

I have two MVC websites. Site 1 has a controller that calls Site 2 with the following code

 //  If I remove this in the controller of site1, then execution continues....
 var asdf =   SharedTypes.Utilities.GetjsonStream("http://localhost:11541/UIDP/Details/a1?format=json");
 string g =  asdf.Result;


public class Utilities
{
    public static async Task<string> GetjsonStream(string url)
    {
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url);
        string content = await response.Content.ReadAsStringAsync();
        Debug.WriteLine("Content: " + content);
        return content;
    }
}

I'm able to directly browse the URL and see the JSON.. but what is the correct way to download JSON from my peer website in MVC?

1 Answer 1

2

You should probably turn the controller method into an async method and use await to avoid deadlocks.

public async Task<ActionResult> MyActionAsync()
{
    var asdf = SharedTypes.Utilities.GetjsonStream(someUrl);
    string g = await asdf;
    // return something
}

Microsoft's ASP.NET tutorial includes a page on async methods in ASP.NET MVC 4.

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

1 Comment

This seems to work well for one project calling another... but I couldn't get it to work for a MVC app to use HTTPWebRequest to call itself. It was probably unneeded overhead anyway. This is great for the two sites communicating with each other ...

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.