如何解决反应路由器被缓存后,无法通过浏览器访问ASP控制器路由
我拥有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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。