Skip to content

Support for 404 Status Code #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
naveedahmed1 opened this issue Sep 28, 2017 · 3 comments
Open

Support for 404 Status Code #425

naveedahmed1 opened this issue Sep 28, 2017 · 3 comments

Comments

@naveedahmed1
Copy link

There should also be a support for setting proper status code especially in case of 404 not found errors.

I have seen some discussion about setting status codes here:

angular/universal#667

And the service referred by @patrickmichalina seems awesome.

https://github.com/patrickmichalina/fusebox-angular-universal-starter/blob/master/src/client/app/shared/services/server-response.service.ts

To add its support I think it would require update to aspnetcore-engine.

@MarkPieszak @isaac2004

@isaacrlevin
Copy link
Contributor

@MarkPieszak did you plan on doing this in engine?

@etatuev
Copy link
Contributor

etatuev commented Dec 11, 2017

Must-have functionality for production use with SEO, voting up.
For those who curious how it can be achieved right now, I made an really ugly hack until this will be implemented.

1) Set
Whenever you need to set custom HTTP status code - add a meta.

@Component({
    selector: 'not-found-page',
    templateUrl: './not-found-page.view.html',
})
export class NotFoundPageComponent {
    constructor(meta: Meta, _config: AppConfig) {
        if (_config.isServer) {
            meta.addTag({ name: 'STATUS_CODE', content: '404' });
        }
    }
}

2) Parse
Parse it at boot.server.ts, and set approciate statusCode when returning preprendering result.

 return ngAspnetCoreEngine(setupOptions).then(response => {

    ....

    // extract STATUS_CODE
    const regExp = new RegExp(/<meta.*?name="STATUS_CODE".*?content="(.*?)".*?\/>/);
    let statusCode = 200;
    const result = regExp.exec(response.globals.meta);
    if (result) {
      statusCode = +result[1]; // take first group
      response.globals.meta = response.globals.meta.replace(result[0], ''); // remove fake meta by full math
    }

    return ({
      html: response.html,
      globals: response.globals,
      statusCode: statusCode,
    });
  });

3) Apply
Extract and set StatusCode to Response object right after prerendering on AspnetCore side.

  // Prerender / Serialize application (with Universal)
  var prerenderResult = await Prerenderer.RenderToString(
           ...
  );
  Response.StatusCode = prerenderResult.StatusCode ?? (int)HttpStatusCode.OK;
  return View();

@naveedahmed1
Copy link
Author

With new Angular CLI based template (aspnet/JavaScriptServices#1288 (comment)), we would need different approach.

I have an open issue at aspnet/JavaScriptServices#1423

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants