How to detect page refresh in .net core angular SPA template?












0















I have project setup with .net core MVC SPA template. When the application is loaded, it loads angular spa into MVC.



My startup configure has below code to load SPA.



        app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Once the SPA is loaded, there is no contact with server side on the MVC site. However when the page is reloaded, I would like to intercept this call and do something with the HTTPRequest and HTTPResponse. How do I achieve this ? I do not have any controller in the MVC project.



My project structure looks like this.



WEB

- wwwroot

- ClientApp ---> Angular spa

- Controllers ---> Empty

- Pages ---> Empty

- startup.cs

- program.cs










share|improve this question























  • Can I ask what you're trying to achieve? Why do you need to detect page reload?

    – matt_lethargic
    Nov 16 '18 at 9:09











  • We need to refresh some of the CSS when page loads. This css will be designed by external tool so we would like to setup this css when the page loads and then SPA can take over and render style as is. If css changed on server then on page load we can again put new css

    – Zeus
    Nov 16 '18 at 19:56











  • What do you mean by when the page is reloaded? By pressing F5?

    – Tao Zhou
    Nov 19 '18 at 5:49
















0















I have project setup with .net core MVC SPA template. When the application is loaded, it loads angular spa into MVC.



My startup configure has below code to load SPA.



        app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Once the SPA is loaded, there is no contact with server side on the MVC site. However when the page is reloaded, I would like to intercept this call and do something with the HTTPRequest and HTTPResponse. How do I achieve this ? I do not have any controller in the MVC project.



My project structure looks like this.



WEB

- wwwroot

- ClientApp ---> Angular spa

- Controllers ---> Empty

- Pages ---> Empty

- startup.cs

- program.cs










share|improve this question























  • Can I ask what you're trying to achieve? Why do you need to detect page reload?

    – matt_lethargic
    Nov 16 '18 at 9:09











  • We need to refresh some of the CSS when page loads. This css will be designed by external tool so we would like to setup this css when the page loads and then SPA can take over and render style as is. If css changed on server then on page load we can again put new css

    – Zeus
    Nov 16 '18 at 19:56











  • What do you mean by when the page is reloaded? By pressing F5?

    – Tao Zhou
    Nov 19 '18 at 5:49














0












0








0








I have project setup with .net core MVC SPA template. When the application is loaded, it loads angular spa into MVC.



My startup configure has below code to load SPA.



        app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Once the SPA is loaded, there is no contact with server side on the MVC site. However when the page is reloaded, I would like to intercept this call and do something with the HTTPRequest and HTTPResponse. How do I achieve this ? I do not have any controller in the MVC project.



My project structure looks like this.



WEB

- wwwroot

- ClientApp ---> Angular spa

- Controllers ---> Empty

- Pages ---> Empty

- startup.cs

- program.cs










share|improve this question














I have project setup with .net core MVC SPA template. When the application is loaded, it loads angular spa into MVC.



My startup configure has below code to load SPA.



        app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Once the SPA is loaded, there is no contact with server side on the MVC site. However when the page is reloaded, I would like to intercept this call and do something with the HTTPRequest and HTTPResponse. How do I achieve this ? I do not have any controller in the MVC project.



My project structure looks like this.



WEB

- wwwroot

- ClientApp ---> Angular spa

- Controllers ---> Empty

- Pages ---> Empty

- startup.cs

- program.cs







.net-core asp.net-core-mvc angular6 single-page-application






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 4:53









ZeusZeus

1,33332942




1,33332942













  • Can I ask what you're trying to achieve? Why do you need to detect page reload?

    – matt_lethargic
    Nov 16 '18 at 9:09











  • We need to refresh some of the CSS when page loads. This css will be designed by external tool so we would like to setup this css when the page loads and then SPA can take over and render style as is. If css changed on server then on page load we can again put new css

    – Zeus
    Nov 16 '18 at 19:56











  • What do you mean by when the page is reloaded? By pressing F5?

    – Tao Zhou
    Nov 19 '18 at 5:49



















  • Can I ask what you're trying to achieve? Why do you need to detect page reload?

    – matt_lethargic
    Nov 16 '18 at 9:09











  • We need to refresh some of the CSS when page loads. This css will be designed by external tool so we would like to setup this css when the page loads and then SPA can take over and render style as is. If css changed on server then on page load we can again put new css

    – Zeus
    Nov 16 '18 at 19:56











  • What do you mean by when the page is reloaded? By pressing F5?

    – Tao Zhou
    Nov 19 '18 at 5:49

















Can I ask what you're trying to achieve? Why do you need to detect page reload?

– matt_lethargic
Nov 16 '18 at 9:09





Can I ask what you're trying to achieve? Why do you need to detect page reload?

– matt_lethargic
Nov 16 '18 at 9:09













We need to refresh some of the CSS when page loads. This css will be designed by external tool so we would like to setup this css when the page loads and then SPA can take over and render style as is. If css changed on server then on page load we can again put new css

– Zeus
Nov 16 '18 at 19:56





We need to refresh some of the CSS when page loads. This css will be designed by external tool so we would like to setup this css when the page loads and then SPA can take over and render style as is. If css changed on server then on page load we can again put new css

– Zeus
Nov 16 '18 at 19:56













What do you mean by when the page is reloaded? By pressing F5?

– Tao Zhou
Nov 19 '18 at 5:49





What do you mean by when the page is reloaded? By pressing F5?

– Tao Zhou
Nov 19 '18 at 5:49












1 Answer
1






active

oldest

votes


















1














For intercepting the request between client and server, you could try ASP.NET Core Middleware. All the requests from client will be handled by middleware.



A simple code like below:



        app.Use((context, next) =>
{
Console.WriteLine(context.Request.Path);
return next.Invoke();
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Update



            app.Map("/css/site1.css", map => {
map.Run(context => {
context.Response.Redirect("/css/site2.css");
return Task.CompletedTask;
});
});





share|improve this answer


























  • I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

    – Zeus
    Nov 20 '18 at 17:35











  • @Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

    – Tao Zhou
    Nov 21 '18 at 1:45











  • There is a requirement to update css on demand i.e. whitelabel

    – Zeus
    Nov 21 '18 at 4:13











  • @Zeus try the update code.

    – Tao Zhou
    Nov 21 '18 at 5:38












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331654%2fhow-to-detect-page-refresh-in-net-core-angular-spa-template%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














For intercepting the request between client and server, you could try ASP.NET Core Middleware. All the requests from client will be handled by middleware.



A simple code like below:



        app.Use((context, next) =>
{
Console.WriteLine(context.Request.Path);
return next.Invoke();
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Update



            app.Map("/css/site1.css", map => {
map.Run(context => {
context.Response.Redirect("/css/site2.css");
return Task.CompletedTask;
});
});





share|improve this answer


























  • I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

    – Zeus
    Nov 20 '18 at 17:35











  • @Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

    – Tao Zhou
    Nov 21 '18 at 1:45











  • There is a requirement to update css on demand i.e. whitelabel

    – Zeus
    Nov 21 '18 at 4:13











  • @Zeus try the update code.

    – Tao Zhou
    Nov 21 '18 at 5:38
















1














For intercepting the request between client and server, you could try ASP.NET Core Middleware. All the requests from client will be handled by middleware.



A simple code like below:



        app.Use((context, next) =>
{
Console.WriteLine(context.Request.Path);
return next.Invoke();
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Update



            app.Map("/css/site1.css", map => {
map.Run(context => {
context.Response.Redirect("/css/site2.css");
return Task.CompletedTask;
});
});





share|improve this answer


























  • I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

    – Zeus
    Nov 20 '18 at 17:35











  • @Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

    – Tao Zhou
    Nov 21 '18 at 1:45











  • There is a requirement to update css on demand i.e. whitelabel

    – Zeus
    Nov 21 '18 at 4:13











  • @Zeus try the update code.

    – Tao Zhou
    Nov 21 '18 at 5:38














1












1








1







For intercepting the request between client and server, you could try ASP.NET Core Middleware. All the requests from client will be handled by middleware.



A simple code like below:



        app.Use((context, next) =>
{
Console.WriteLine(context.Request.Path);
return next.Invoke();
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Update



            app.Map("/css/site1.css", map => {
map.Run(context => {
context.Response.Redirect("/css/site2.css");
return Task.CompletedTask;
});
});





share|improve this answer















For intercepting the request between client and server, you could try ASP.NET Core Middleware. All the requests from client will be handled by middleware.



A simple code like below:



        app.Use((context, next) =>
{
Console.WriteLine(context.Request.Path);
return next.Invoke();
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});


Update



            app.Map("/css/site1.css", map => {
map.Run(context => {
context.Response.Redirect("/css/site2.css");
return Task.CompletedTask;
});
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 5:38

























answered Nov 19 '18 at 5:51









Tao ZhouTao Zhou

7,37431434




7,37431434













  • I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

    – Zeus
    Nov 20 '18 at 17:35











  • @Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

    – Tao Zhou
    Nov 21 '18 at 1:45











  • There is a requirement to update css on demand i.e. whitelabel

    – Zeus
    Nov 21 '18 at 4:13











  • @Zeus try the update code.

    – Tao Zhou
    Nov 21 '18 at 5:38



















  • I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

    – Zeus
    Nov 20 '18 at 17:35











  • @Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

    – Tao Zhou
    Nov 21 '18 at 1:45











  • There is a requirement to update css on demand i.e. whitelabel

    – Zeus
    Nov 21 '18 at 4:13











  • @Zeus try the update code.

    – Tao Zhou
    Nov 21 '18 at 5:38

















I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

– Zeus
Nov 20 '18 at 17:35





I am able to intercept this way but can I insert CSS through app.use() { } inside the angular page ?

– Zeus
Nov 20 '18 at 17:35













@Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

– Tao Zhou
Nov 21 '18 at 1:45





@Zeus What do you mean by insert css? if you want to change the css url, you could try URL Rewriting Middleware in ASP.NET Core, but it is not recommended which will cause performance. You should change the css url in the page instead of intercept the reuqest.

– Tao Zhou
Nov 21 '18 at 1:45













There is a requirement to update css on demand i.e. whitelabel

– Zeus
Nov 21 '18 at 4:13





There is a requirement to update css on demand i.e. whitelabel

– Zeus
Nov 21 '18 at 4:13













@Zeus try the update code.

– Tao Zhou
Nov 21 '18 at 5:38





@Zeus try the update code.

– Tao Zhou
Nov 21 '18 at 5:38




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331654%2fhow-to-detect-page-refresh-in-net-core-angular-spa-template%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma