Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have two solutions
1. ASP.NET web application(.Net framework)(Web Api)
2. ASP.NET Core application(.Net Core)

in Visula Studio 17.


import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions, RequestMethod } from '@angular/http';
import 'rxjs/Rx';

import { Employee } from '../models/Employee'

@Injectable()
export class EmployeeManagementService {
    constructor(private http: Http) {
    }    
}

    addEmployeeDetails(employeeDetails: Employee) {

        var obj = {Firstname: employeeDetails.FirstName, LastName: employeeDetails.LastName, PhoneNumber: employeeDetails.PhoneNumber};

        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
        let options = new RequestOptions({ method: RequestMethod.Post, headers: headers });

        let emp = JSON.stringify({ obj });

        return this.http.post('http://localhost:xyz/api/employee-management/create-employee', emp, options)
            .map((res: Response) => res.json())
            .toPromise();



public class EmployeeManagementApiController : ApiController
{
        //Create Employee
        [HttpPost]
        [Route("api/employee-management/create-employee")]
        public IHttpActionResult CreateEmployee([FromBody]Employee emp)
        {
            EmployeeService service = new EmployeeService();
            var serv = service.CreateEmployee(emp);

            if (serv.status == 1)
            {
                return Ok(emp);
            }
            else
                return new ResponseMessageResult(Request.CreateResponse(HttpStatusCode.PreconditionFailed, serv.message));
        }
}

Employee.ts file

<pre>
   public class Employee
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string PhoneNumber { get; set; }
    }

<pre>
 export class Employee {
    FirstName: string;
    LastName: string;
    PhoneNumber: string;
}


My ts file is in on solution and my api is in another solution.
here am getting calls to my API but emp values are all null. Am i missing anything . Please help me out.

Thanks in advance

What I have tried:

same code i have tried with normal controller in same Solution ,
if i send single parameter values is passed to API controller.
Posted
Updated 15-May-17 23:12pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900