简体   繁体   中英

how to read JSON data from url sent from Angular 2 using asp.net

I am developing a simple Angular 2 reactive form. I am using ASP.NET and SQL server as my backend to store form data. I send the form data from angular 2 using angular service to asp.net page, there I can't able to read those form data.

This is my angular code to send form data.

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from  'rxjs/observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import { User } from './signup.interface';

@Injectable()
export class SignupService {
    headers: Headers;
    options: RequestOptions;

    private producturl = "http://172.21.41.70:8081/DB-Connect/";
    constructor(private _http: Http) {
        this.headers = new Headers({
            'Content-Type': 'application/json'
         });
          this.options = new RequestOptions({ headers: this.headers });
    }

    postDetails(user: User): Observable<User[]> {
       // console.log(user); 
        let body = JSON.stringify(user);
        console.log(body);
        return this._http.post(this.producturl, body, this.options)
            .map((response: Response) => response)
            .do(data => console.log(JSON.stringify(data)))
            .catch(this.handleError);
    }

    /*private extractData(res: Response) {
        console.log(res);
        let body = res.json();
        return body || {};
    }*/

    private handleError(error: Response) {
        console.log(error);
        return Observable.throw(error.json().error());
    }
}

This my asp.net code

   Response.ContentType = "text/plain";
        using (WebClient wc = new WebClient())
        {
            var json = wc.DownloadString("http://localhost:4200");
            Response.Write(json);
        }

While execute the code i am getting angular index page completely as a output.I am not getting any JSON data which I sent.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Demo</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script><script type="text/javascript" src="polyfills.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script><script type="text/javascript" src="vendor.bundle.js"></script><script type="text/javascript" src="main.bundle.js"></script></body>
</html>

Please help me to read JSON data in asp.net page. Thanks in advance

The Angular 2 project with .NET as backend can be done with Visual studio 2017 and .NET SDK framework installed.

  1. Install visual studio 2017.
  2. install .net sdk framework.
  3. Go to File -> New -> Project -> ASP .NET core web application and give okay.
  4. Then select Angular to create our Angular application.
  5. Code Angular part inside ClientApp folder. It also contain two demo files for our reference.
  6. Create one C# file inside Controllers folder for backend access.For example I created the file named ValuesController.cs
  7. We can call that C# controller file from Angular service file as 'api/Values' (Please refer this URL in ValuesController.cs)
  8. We can code our backend code inside the ValuesController.cs file inside individual get,post functions. In that controller file we can get and post data.

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM