[英]ASP.NET 5 MVC 6: How to configure startup to send a html file when not on any mvc route
I'm building a SPA application using React-Router to handle the front-end routing. 我正在使用React-Router构建SPA应用程序来处理前端路由。
I'm trying to configure the ASP.NET 5 Startup.cs file to use MVC but when a route does not match any of my API routes to send the index.html file so that it will be able to handle react-routers browser history implementation as stated here . 我正在尝试配置ASP.NET 5 Startup.cs文件以使用MVC,但是当路由与我的任何API路由都不匹配时,发送index.html文件以便它能够处理react-routers浏览器历史记录作为执行陈述这里 。
So far i've only been able to get it to send the index.html file when on the default route, ie nothing after the localhost:3000. 到目前为止,我只能在默认路由上发送index.html文件,即在localhost:3000之后没有发送。
My Startup Configure method so far. 到目前为止我的启动配置方法。 Thank you
谢谢
app.UseIdentity();
app.UseIISPlatformHandler();
app.UseDefaultFiles(new DefaultFilesOptions {
DefaultFileNames = new List<string> { "index.html"}
});
app.UseStaticFiles();
app.UseMvc();
Pretty simple, with app.UseStatusCodePagesWithReExecute("/");
非常简单,使用
app.UseStatusCodePagesWithReExecute("/");
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseStatusCodePagesWithReExecute("/");
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
});
}
This will return the index.html
page in your wwwroot
, even if you get a 404. It will however only change the page, but not the URL. 这将返回
wwwroot
的index.html
页面,即使您获得404.但它只会更改页面,而不会更改URL。
Alternatively , you might not want to do a page redirect since you are doing a SPA, but if you really need to update the URL you can just do a simple redirect from a controller, like so: 或者 ,您可能不希望执行页面重定向,因为您正在执行SPA,但如果您确实需要更新URL,则可以从控制器执行简单的重定向,如下所示:
public class RedirectController : Controller
{
public IActionResult Index()
{
return Redirect(Url.Content("~/"));
}
}
and then in your startup update this: 然后在你的启动更新这个:
app.UseStatusCodePagesWithReExecute("/redirect");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.