Skip to content
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

Http Service模式下,前端路由刷新后页面会找不到 #49

Open
1 task done
Leoc82 opened this issue Dec 19, 2024 · 1 comment
Open
1 task done

Http Service模式下,前端路由刷新后页面会找不到 #49

Leoc82 opened this issue Dec 19, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Leoc82
Copy link

Leoc82 commented Dec 19, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Http Service模式下,前端路由刷新后页面会找不到,例如用react和angular这种带有前端路由的SPA页面,如果在带有路由的页面上进行强刷,就是找不到页面。

Describe the solution you'd like

他们一般靠服务器端的rewrite功能解决这个问题,是否可以做类似的功能像是加一个.htaccess文件,里面写上rewrite的规则

Additional context

No response

@Leoc82 Leoc82 added the enhancement New feature or request label Dec 19, 2024
@RRQM
Copy link
Owner

RRQM commented Dec 22, 2024

感谢您的反馈!

你可以使用下面代码替换逻辑:

.ConfigurePlugins(a =>
{
    a.UseHttpStaticPage(option =>
    {
        var filesPool = option.FilesPool;
        option.SetNavigateAction((request) =>
        {
            var relativeURL = request.RelativeURL;
            var url = relativeURL;

            if (filesPool.ContainsEntry(url))
            {
                return url;
            }

            if (relativeURL.EndsWith("/"))
            {
                url = relativeURL + "index.html";
                if (filesPool.ContainsEntry(url))
                {
                    return url;
                }
            }
            else if (relativeURL.EndsWith("index"))
            {
                url = relativeURL + ".html";
                if (filesPool.ContainsEntry(url))
                {
                    return url;
                }
            }
            else
            {
                url = relativeURL + "/index.html";
                if (filesPool.ContainsEntry(url))
                {
                    return url;
                }
            }
            return "/index.html";
        });
    })
     .AddFolder("www");

    // 默认插件应该最后添加,其作用是
    // 1、为找不到的路由返回404
    // 2、处理header为Option的跨域请求。
    a.UseDefaultHttpServicePlugin();
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants