sonal malhotra

sonal malhotra

  • NA
  • 410
  • 52k

error(blogservice) in angular with web api please solve

Jul 4 2019 5:29 AM
hello expert,
 
i m sharing coding in angular in web api method code are here ..
  1. public HttpResponseMessage GetId(string id)  
  2. {  
  3. //blogtaable objEmp = new blogtaable();  
  4. int ID = Convert.ToInt32(id);  
  5. angDB.blogtaables.Where(x => x.blogid == ID).Single();  
  6. return ToJson(angDB.SaveChanges());  
  7. }  
i am not sure is this correct ? i want to display blog details in webapi controller
 
how to write syntax/code in web api method(GetId)?
 
here is blogservice
  1. Getid(url: string, id: any): Observable<Iblog> {  
  2. let headers = new Headers({ 'Content-Type''application/json' });  
  3. let options = new RequestOptions({ headers: headers });  
  4. return this._http.get(url + id, options)  
  5. .map((response: Response) => <Iblog>response.json())  
  6. .catch(this.handleError);  
Error is shown in blog service
 
here is blog details .ts
  1. import { Component, OnInit } from '@angular/core';  
  2. import { RouterModule, ActivatedRoute } from '@angular/router';  
  3. import { blogService } from '../Service/blogservice';  
  4. import { Observable } from 'rxjs/Observable';  
  5. import { Iblog } from '../model/blog';  
  6. import { Global } from '../Shared/global';  
  7. @Component({  
  8. selector: 'employee-details',  
  9. templateUrl: 'app/components/blog-details.component.html',  
  10. providers: [blogService],  
  11. })  
  12. export class blogDetailsComponent implements OnInit {  
  13. private sub: any;  
  14. private blog: Iblog;  
  15. constructor(private employeeService: blogService, private route: ActivatedRoute) {  
  16. }  
  17. ngOnInit() {  
  18. // Subscribe to route params  
  19. this.sub = this.route.params.subscribe(params => {  
  20. let id = params['id'];  
  21. return this.employeeService.Getid(Global.BASE_USER_ENDPOINT,id).subscribe((blogs1 => { this.blog = blogs1; }));  
  22. });  
  23. }  
  24. }  
here is blog details in html
  1. <div *ngIf="blog">  
  2. <h2>Blog Details</h2>  
  3. <table>  
  4. <tr>  
  5. <td>Title:</td>  
  6. <td>{{blog.Title}}</td>  
  7. </tr>  
  8. <tr>  
  9. <td>Date:</td>  
  10. <td>{{blog.dateofpublish}}</td>  
  11. </tr>  
  12. <tr>  
  13. <td>content:</td>  
  14. <td>{{blog.content1}}</td>  
  15. </tr>  
  16. </table>  
  17. <a [routerLink]="['/blogs']">Back</a>  
  18. </div>  
here is routing..
  1. import { ModuleWithProviders } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { blogComponent } from './components/blog';  
  4. import { HomeComponent } from './components/home';  
  5. import { blogDetailsComponent} from './components/blog-details.components';  
  6. const appRoutes: Routes = [  
  7. { path: '', redirectTo: 'home', pathMatch: 'full' },  
  8. { path: 'home', component: HomeComponent },  
  9. { path: 'blog', component: blogComponent },  
  10. { path: 'blog/:id', component: blogDetailsComponent },  
  11. ];  
  12. export const routing: ModuleWithProviders =  
  13. RouterModule.forRoot(appRoutes);  
please share idea for error .. if i did wrong something then please share ur code or repair

Answers (2)