% symbol encoded to %25 in URL every reload

3.1k views Asked by At

İ use Spring-Data-Rest and Angular 2

% URL encode code %25 (http://www.w3schools.com/tags/ref_urlencode.asp)

Firstly;

http://localhost:4200/members/http%3A%2F%2Flocalhost%3A8080%2FErPApI%2Forganizations%2F1/detail

when navigate this URL and reload page again or copy paste new tab link

% encode again %25. İts doing every reload.

http://localhost:4200/members/http%253A%252F%252Flocalhost%253A8080%252FErPApI%252Forganizations%252F1/detail

So i lost member id. How can i block encode % again?

2

There are 2 answers

1
vangoo On BEST ANSWER

I used the following method when I get the link.

 decodeURIComponent()
0
Margaux On

I add provider custom UrlSerializer to root AppModule.

Like this...

// custom-url-serializer.ts
import { DefaultUrlSerializer, UrlSerializer, UrlTree } from '@angular/router';
export class CustomUrlSerializer implements UrlSerializer {
    private defaultUrlSerializer: DefaultUrlSerializer = new DefaultUrlSerializer();

    parse(url: string): UrlTree {
        url = url.replace(/\%/g, '%25');
        return this.defaultUrlSerializer.parse(url);
    }

    serialize(tree: UrlTree): string {
        return this.defaultUrlSerializer.serialize(tree).replace(/%25/g, '%');
    }
}



// app.module.ts
import { NgModule } from '@angular/core';
import { CustomUrlSerializer } from './shared/custom-url-serializer';

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        ...
    ],
    bootstrap: [AppComponent],
    providers: [
        {
            provide: UrlSerializer,
            useClass: CustomUrlSerializer,    // <-- Here!
        },
    ],
})
export class AppModule { }