反应路由器被缓存后,无法通过浏览器访问ASP控制器路由

时间:2020-09-11 19:19:25

标签: asp.net reactjs react-router-v4

我拥有ASP.NET Core控制器以及react spa应用程序。 ASP使用控制器路由,而spa使用react-router。一旦至少访问一次React应用程序,浏览器就不再通过搜索栏接受到ASP控制器的路由,并自动定向到默认的React页面。

清除浏览器缓存后,我可以通过在浏览器中直接键入url来访问asp路由,直到再次访问react应用为止。

react应用通过提取对asp控制器发出的请求仍然有效。

我的最终目标是能够从react应用重定向到控制器中的“ / authentication / login”,然后可以重定向到外部登录。目前<a href='/authentication/login'>Login</a><Nav.Link as={Link} to='/authentication/login'>Login</a>都不起作用。

我怀疑这可能是一个类似的问题,但我不知道如何解决:https://stackoverflow.com/a/51484292/9885454

此解决方案无济于事:https://stackoverflow.com/a/60421359/9885454

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
        .AddCookie(options =>
        {
            options.LoginPath = "/authentication/login";
            options.LogoutPath = "/authentication/logout";
        })
        .AddDiscord(options =>
        {
            options.ClientId = Configuration["Discord:ClientId"];
            options.ClientSecret = Configuration["Discord:ClientSecret"];
        });

    services.AddControllers();
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/build";
    });
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeli
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseSpaStaticFiles();

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller}/{action=Index}/{id?}");
    });

    app.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp";
        if (env.IsDevelopment())
        {
            spa.UseReactDevelopmentServer(npmScript: "start");
        }
    });
}

App.js

export default class App extends Component {
    static displayName = App.name;

    render() {
        return (
            <>
                <Layout>
                    <Switch>
                        <Route exact path='/' component={GaTracker(Home)} />
                        <Route path='/commands' component={GaTracker(Commands)} />
                        <Route path='/guide' component={GaTracker(Guide)} />
                        <Route path='/user/me' component={GaTracker(User)} />

                        <Redirect to='/' />
                    </Switch>
                </Layout>
            </>
        );
    }
}

Index.js

import 'bootstrap/dist/css/bootstrap.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

export const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
const rootElement = document.getElementById('root');

ReactDOM.render(
    <BrowserRouter basename={baseUrl}>
        <App />
    </BrowserRouter>,
    rootElement);
registerServiceWorker();

0 个答案:

没有答案
相关问题