Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在谷歌云应用引擎上重写url?_Reactjs_Google App Engine_Url Rewriting_.net Core_React Router - Fatal编程技术网

Reactjs 如何在谷歌云应用引擎上重写url?

Reactjs 如何在谷歌云应用引擎上重写url?,reactjs,google-app-engine,url-rewriting,.net-core,react-router,Reactjs,Google App Engine,Url Rewriting,.net Core,React Router,我将我的客户端react应用程序和dot net core API部署在谷歌云应用程序引擎的同一服务和同一版本上。这是我第一次使用AppEngine。通常,当我在IIS上部署时,我会有一个web配置来使用react路由重写Url,但似乎Url重写不适用于应用程序引擎。是否有其他方法可以使用IIS获得相同的结果?欢迎使用App Engine。Google Cloud App Engine Flex不运行IIS,因此它不会读取web.config的内容 我发现了这篇关于重写ASP.NET core中

我将我的客户端react应用程序和dot net core API部署在谷歌云应用程序引擎的同一服务和同一版本上。这是我第一次使用AppEngine。通常,当我在IIS上部署时,我会有一个web配置来使用react路由重写Url,但似乎Url重写不适用于应用程序引擎。是否有其他方法可以使用IIS获得相同的结果?

欢迎使用App Engine。Google Cloud App Engine Flex不运行IIS,因此它不会读取web.config的内容

我发现了这篇关于重写ASP.NET core中间件的文章: 我相信这个中间件可以让你根据需要重写URL,但是如果没有你的web.config,我不确定


使用重写中间件而不是web.config的应用程序在IIS和App Engine上的行为相同。

如果使用ASP.NET Core,您可以尝试这种方法吗

添加此类:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Rewrite;
using System;

namespace APA.Web.Common
{
    public class RedirectToWwwRule : IRule
    {
        public virtual void ApplyRule(RewriteContext context)
        {
            var req = context.HttpContext.Request;
            if (req.Host.Host.Equals("localhost", StringComparison.OrdinalIgnoreCase))
            {
                context.Result = RuleResult.ContinueRules;
                return;
            }

            if (req.Host.Value.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
            {
                context.Result = RuleResult.ContinueRules;
                return;
            }

            var wwwHost = new HostString($"www.{req.Host.Value}");
            var newUrl = UriHelper.BuildAbsolute(req.Scheme, wwwHost, req.PathBase, req.Path, req.QueryString);
            var response = context.HttpContext.Response;
            response.StatusCode = 301;
            response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Location] = newUrl;
            context.Result = RuleResult.EndResponse;
        }
    }
}
然后在Startup.cs的Configure方法中使用它

添加此代码以使用它:

using Microsoft.AspNetCore.Rewrite;   



public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      ...
      ...

    var options = new RewriteOptions();
    options.AddRedirectToHttps();
    options.Rules.Add(new RedirectToWwwRule());
    app.UseRewriter(options);
...
我使用此代码将所有非www/HTTP流量重定向到www/HTTPS版本