Asp.Net Core 2端点被击中两次

时间:2017-12-22 23:27:19

标签: angular asp.net-core-2.0

问题:我在asp.net core 2控制器中的端点被我的角度4客户端JS击中了两次。

组件HTML:

<h1>Login To Spotify</h1>

<button type="button" (click)="loginToSpotify()">Boop</button>
<p>{{_logger}}</p>

loginToSpotify会将当前页面重定向到自己,其中包含URL查询字符串,代码和状态中的2个参数,这些参数由我下面的下一个HTTP发布请求使用。

组件方法:

ngOnInit() {
    this.checkPostBack(this._router.url);
}

private checkPostBack(url: string) {
    let urlTree: UrlTree = this._router.parseUrl(url);

    this._spotifyRedirect = {
        code: urlTree.queryParamMap.get('code'),
        state: urlTree.queryParamMap.get('state')
    };

    if (this._spotifyRedirect.code === null && this._spotifyRedirect.state === null) {
        this._logger = "No code or state provided!";
        return;
    }

    let spotifyAccessTokenResponse: SpotifyAccessTokenResponse;

    this._spotifyService.getAccountAccessToken('example', this._spotifyRedirect.code, 'http://localhost:52028/spotifytoyoutube')
        .subscribe(result => {
            this._logger = "Logged into spotify successfully! ";
            spotifyAccessTokenResponse = result;
        }, error => {
            this._logger = "Error: " + error;
        });
}

服务方式:

getAccountAccessToken(grantType: string | null, code: string | null, redirectUri: string) : Observable<SpotifyAccessTokenResponse> {
    let queryEncoder: QueryEncoder = new QueryEncoder();

    let spotifyAccessTokenRequest: SpotifyAccessTokenRequest = {
        grant_type: grantType,
        code: code,
        redirect_uri: queryEncoder.encodeValue(redirectUri)
    }

    return this.http
        .post(this.baseUrl + 'api/Spotify/AccessToken', spotifyAccessTokenRequest)
        .map(result => {
            return <SpotifyAccessTokenResponse>result.json();
        })
        .catch(this.handleError)
        .share();
}

当我断开我的asp.net控制器端点时,它会被击中两次,并且在我的应用程序中没有其他对此终点的引用。我在角度做错了吗?当通过Fiddler检查网页时,它只向我的终点发射一个HTTP Post,但它被断点击中两次。添加一些console.log语句只能在控制台中运行一次。

有什么想法吗?

0 个答案:

没有答案