1

I've been making a project generated many things from out of the database with HTTP request. This has 3 larger queries into 1 C# class

    public IEnumerable<Menu> Menus { get; set; }
    public Dictionary<int, Pagina> Paginas { get; set; }
    public Dictionary<int, Opmaak> Opmaak { get; set; }
    public string Errors { get; set; }

This works like a charm, Main Object

https://i.sstatic.net/V7tNj.jpg - HTTP Network Summary

    public Main Get()
    {
        return Main.getAll();
    }

This can't return anthing, Same Main Object

https://i.sstatic.net/koOfu.jpg - HTTP Network Summary

    public Main Get(string username, string password)
    {
        Login value = new Login(username: username, password: password);
        return Main.getAll(value);
    }

Question, How do I properly send variables over a HTTP request


Webconfig C#

    public static void Register(HttpConfiguration config)
    {
        EnableCrossSiteRequests(config);
        AddRoutes(config);

    }

    private static void AddRoutes(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{username}/{password}",
            defaults: new { username = RouteParameter.Optional, password = RouteParameter.Optional}
        );
    }

    private static void EnableCrossSiteRequests(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute(
            origins: "http://lvh.me:4200",
            headers: "*",
            methods: "*");
        config.EnableCors(cors);
    }

C# Main.cs

 public static Main getAll()
 {
    using (CheckPlusEntities db = new CheckPlusEntities())
    {
        Main Totaal = new Main();

        //Filling Totaal 

        return Totaal;
    }
}

 public static Main getAll(Login Login)
 {
     using (CheckPlusEntities db = new CheckPlusEntities())
     {
         Main Totaal = new Main();

        //Filling Totaal 

        return Totaal;
     }
 }

Angular 2 Service

Wrong =

    get = {


            event: (username:string, password:string): Promise<any> => {    


            let url = "http://{URL}:8080/CheckPlusApi/api/Main/Get/" + username + "/" + password;
            return this._http.get(url)
                .map(response => {
                    return response.json(); // Has a value
                },
                   error =>  {
                       this.errorMessage = <any>error
                }).toPromise();
            }
        }  

Good =

    getTest = {
            event: (): Promise<any> => {            
                return this._http.get('http://{URL}:8080/CheckPlusApi/api/Main/Get')
                .map(response => {
                    return response.json(); 
                },
                   error =>  {
                       this.errorMessage = <any>error
                }).toPromise();
            }     
    };  
17
  • 1
    Is your problem the error that is returned in the image you posted? It says your DbContext has been disposed but you're trying to use it anyway... Commented Dec 22, 2016 at 8:23
  • @silkfire Hello, thanks for the reaction. Both functions open with a using(db) using (CheckPlusEntities db = new CheckPlusEntities()) { Main Totaal = new Main(); Commented Dec 22, 2016 at 8:27
  • Can you mention the client side Java Script code snippet? Actual error could be how you are passing the data. Commented Dec 22, 2016 at 8:30
  • @SachinGaur Added to the post Commented Dec 22, 2016 at 8:33
  • @Kapein Check in your stack trace where exactly the error occurs. Also, your methods says they should return an object of type Main but you never return anything. Commented Dec 22, 2016 at 8:33

1 Answer 1

1

I think the problem lies in your using clause.

Try removing it by changing:

using (CheckPlusEntities db = new CheckPlusEntities())

to this:

CheckPlusEntities db = new CheckPlusEntities();
Sign up to request clarification or add additional context in comments.

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.