简体   繁体   English

页面刷新后的cookie计数为零,并且使用url重写进行角度路由

[英]count of cookies is zero after page refresh and angular routing with url rewrite

I am using angular Spa template with .net core 2.0 and angular 4. 我正在使用.net core 2.0和angular 4的角度Spa模板。

I am getting zero count of cookies after page refresh. 页面刷新后,我的cookie数量为零。 and in fact I am not getting anything in HttpContext after page gets refreshed. 事实上,在刷新页面后,我在HttpContext中没有得到任何东西。

Here is my cookie in browser. 这是我在浏览器中的cookie。 在此输入图像描述

it's working fine if I uses angular system menu. 如果我使用角度系统菜单,它工作正常。

Now, here is my code for getting cookie in back end. 现在,这是我在后端获取cookie的代码。

    private string GetCookie(HttpContext httpContext, string cookieName)
    {
        var rqstCookie = httpContext.Request.Cookies[cookieName];

        return !string.IsNullOrEmpty(rqstCookie) ? rqstCookie : null;
    }

one important thing is that when I click in menu, it loads component and service files and then it requests to C# controller with all headers and options. 一个重要的事情是,当我点击菜单时,它会加载组件和服务文件,然后它向C#controller请求所有标题和选项。 but when I refresh page this whole process get skipped, so I think this options are not delivered to back end and that's why my cookies are empty. 但是当我刷新页面时,整个过程都会被跳过,所以我认为这些选项不会传递到后端,这就是为什么我的cookie是空的。

in my other applications, I have used url rewrite for same kind of issues and it worked over there but here because this application is angular spa template, url rewrite is not working. 在我的其他应用程序中,我已经使用url重写同样的问题,它在那里工作,但在这里,因为这个应用程序是角度spa模板,url重写不起作用。

I have also tried to setup the url rewrite, the same way I did in my old applications, but it gives me error. 我也尝试设置url重写,就像我在旧应用程序中所做的那样,但它给了我错误。 here is the link of the url rewrite module that I followed. 这是我遵循的url重写模块的链接。 https://stackoverflow.com/a/26152011/5816119 https://stackoverflow.com/a/26152011/5816119

I have updated cookies to Session cookie but it's also not working. 我已将Cookie更新为Session Cookie,但它也无效。

can you guide me how to get all the cookies and other data even when user refreshes page. 你能指导我如何获取所有的cookie和其他数据,即使用户刷新页面。 thanks... 谢谢...

It was a angular side issue. 这是一个有棱角的问题。

when I added APP_BASE_HREF in my routing module, it suddenly started working. 当我在路由模块中添加APP_BASE_HREF时,它突然开始工作了。

https://stackoverflow.com/a/34535256/5816119 https://stackoverflow.com/a/34535256/5816119

Here is my code, 这是我的代码,

import { APP_BASE_HREF } from "@angular/common";

export const sharedConfig: NgModule = {
    bootstrap: [ AppComponent ],
    declarations: [
        AppComponent,
        ...
        ],
    imports: [
        HttpModule,
        ...,
        RouterModule.forRoot([
            { path: "", redirectTo: "home", pathMatch: "full" },
            { path: "**", redirectTo: "home" }
        ], { useHash: true }),
    ],
    providers: [
        { provide: APP_BASE_HREF, useValue: '/' },
    ],
};

So, it's basically a hashing technology in router side, and seems I was missing this provider in my app too. 所以,它基本上是路由器端的哈希技术,似乎我在我的应用程序中也缺少这个提供程序。

try to specify when the cookie will expire by using this code when writing cookie 尝试在编写cookie时使用此代码指定cookie何时到期

 option.Expires = DateTime.Now.AddDays(x);

 Response.Cookies.Append(cookiename, cookievalue, option);

you can modify AddDays(x) to the number of days you want 您可以将AddDays(x)修改为所需的天数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM