简体   繁体   English

ASP.NET 5 MVC 6:如何配置启动以在不在任何mvc路由上时发送html文件

[英]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. 这将返回wwwrootindex.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.

相关问题 ASP.Net MVC:如何使用 2 个参数配置动作路由? - ASP.Net MVC: How to configure route for action with 2 parameters? 在ASP.NET MVC中配置HTML压缩 - Configure HTML Minification in ASP.NET MVC 如何在启动文件上配置基于属性的路由,而不是在 asp.net 核心 mvc 中使用 controller 动作上的属性 - How can i configure attribute based routing on startup file instead of using attribute on controller action in asp.net core mvc 获取html文件内容并将其发送到asp.net mvc中的页面 - get the html file content and send it to page in asp.net mvc 当调用ConfigureServices和Configure方法时,在startup.cs中的asp.net core mvc(dotnet 5)中? - in asp.net core mvc (dotnet 5) in startup.cs when ConfigureServices and Configure method called? 如何在Asp.net MVC中配置外部配置文件? - How to configure external config file in Asp.net MVC? 在ASP.NET MVC中使用区域时如何配置路由? - How to configure routes when using areas in asp.net mvc? 当路由缺少asp.net MVC中的“部分”时,如何重定向? - How to redirect when route is missing “parts” in asp.net MVC? 如何将HTML内容发送到ASP.NET MVC JSON函数? - How to send HTML content to ASP.NET MVC JSON function? 如何在ASP.NET MVC中定义此路由? - How to define this route in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM