Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 not able to call Custom Web Api using asp.net MVC 5 Web Api

I have a problem calling Custom Web Api in asp.net MVC 5.The following is my code for Web Api Controller and angular js 2.

 [Route("api/email/detail/{id:int}"), HttpGet]
 public async Task<IHttpActionResult> EmailDetail(int id)
 {
     return Ok();
 }

The code for angular app.routing.ts is as follows:

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './components/home.component';
import { UserComponent } from './components/user.component';
import { EmailComponent } from './components/email.component';
import { EmailDetail } from './components/email.detail';

const appRoutes: Routes = [
    { path: 'App/Template', redirectTo: 'home', pathMatch: 'full' },
    { path: 'home', component: HomeComponent },
    { path: 'user', component: UserComponent },
    { path: 'detail/:id', component: EmailDetail },
    { path: 'email', component: EmailComponent },
    //{ path: 'detail', component: EmailDetail},
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

Code for email.detail.ts is as follows:

import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { UserService } from '../Service/user.service';
import { IEmail } from '../Models/user';

import { Http, Response, Headers, RequestOptions } from '@angular/http';


@Component({
    templateUrl: 'About/Template/Detail'
})

export class EmailDetail implements OnInit {
    private emailId: number;
    private email: IEmail

    constructor(private _userService: UserService, private _http: Http, 
    private route: ActivatedRoute) {

    }

    ngOnInit(): void {

        this._http.get("api/email/detail/1992").subscribe(data => {
            // Read the result field from the JSON response.
        });

    }
}

The error I get in the console is as follows: The api call is http://localhost:16552/detail/api/email/detail/1992 and detail is getting prepended which is preventing from calling the api controller.

like image 279
Pankaj Dalvi Avatar asked Jun 03 '26 11:06

Pankaj Dalvi


1 Answers

useHash solved the problem and its working fine now.

like image 190
Pankaj Dalvi Avatar answered Jun 06 '26 07:06

Pankaj Dalvi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!