ASP.Net Core 3.0(3.1)为单页应用程序设置PathBase [英] ASP.Net core 3.0 (3.1) set PathBase for single page application

查看:1163
本文介绍了ASP.Net Core 3.0(3.1)为单页应用程序设置PathBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我配置了以下asp.net核心spa应用程序(react-redux模板)

  public void Configure(IApplicationBuilder app ,IWebHostEnvironment env)
{
app.UsePathBase(new PathString( / foo));

if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler( / Error);
}

app.UseStaticFiles();
app.UseSpaStaticFiles();

app.UseRouting();

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);
}
});
}

我想为应用程序设置pathBase,但是 app.UsePathBase(new PathString( / foo))被忽略。在2.2上,它运行良好。自动修改的index.html和所有静态文件都移到了相对路径。但是在3.0(3.1)上,将静态+生成的文件放在根目录上。



。Net Core 2.2上的生成文件



。Net Core 3.0上的生成文件



有人对解决它有任何想法吗?还是可能是一些使用pathBase的Startup.cs示例?

解决方案


  1. 通常,使用 app.UsePathBase(new PathString( / foo)); 是因为反向代理切断了某些前缀并导致ASP.NET Core应用程序无法实现虚拟应用程序路径以 / foo 开头。在您的方案中,如果没有反向代理将前缀重写为空字符串,则不需要 app.UsePathBase(...)


  2. 相反,如果您的spa在子路径上运行,则可以设置一个中间件来分支 / foo


  3. 最后,您可能想在中添加主页的属性package.json ,以便在发布时将生成< base url = / foo / /> 。或者,您也可以更新 ClientApp / public / index.html < base url => c>手动。

简而言之,添加首页: / foo / 在您的 package.json

 
private中:true,
主页: / foo /,
依赖项:{
...
}

并设置 / foo 分支以使SPA在该路径下运行:



< pre class = lang-cs prettyprint-override> string spaPath = / foo;
app.Map(spaPath,appBuilder => {
appBuilder.UseSpa(spa =>
{
spa.Options.DefaultPage = spaPath + / index.html;
spa.Options.DefaultPageStaticFileOptions =新的StaticFileOptions {
RequestPath = spaPath,
};
spa.Options.SourcePath = ClientApp;
if(env.IsDevelopment ())
{
spa.UseReactDevelopmentServer(npmScript: start);
}
});
});

不要使用 app.UsePathBase(new PathString( / foo )),除非您确实希望覆盖所有路线的路径基础。


I have the following asp.net core spa application configured (react-redux template)

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UsePathBase(new PathString("/foo"));

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseRouting();

            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");
                }
            });
        } 

I’d like to set pathBase for application, but app.UsePathBase(new PathString("/foo")) just ignored. On 2.2 it perfectly worked. Automatically modified index.html and all static files were moved to relative path. But on 3.0 (3.1) static + generated files are placed on root.

Generated files on .Net Core 2.2

Generated files on .Net Core 3.0

Does anyone have any ideas for solving it? Or may be some examples of Startup.cs with working pathBase?

解决方案

  1. Usually, app.UsePathBase(new PathString("/foo")); is used because the reverse proxy cuts off some prefix and causes ASP.NET Core app doesn't realize the virtual app path is start with /foo. In your scenario, if you don't have a reverse proxy that rewrite the prefix to empty string, you don't need app.UsePathBase(...).

  2. Instead, if you your spa runs on a subpath, you could setup a middleware that branches the /foo.

  3. Finally, you might want to add a property of homepage in your package.json so that it will generate the <base url="/foo/"/> when publishing. Or as an alternative, you could update the <base url=""> in ClientApp/public/index.html manually.

In short, add a "homepage": "/foo/" in your package.json

  "private": true,
  "homepage": "/foo/",
  "dependencies": {
      ...
   }

And setup a /foo branch to make SPA runs under that path:

string spaPath = "/foo";
app.Map(spaPath,appBuilder =>{
    appBuilder.UseSpa(spa =>
    {
        spa.Options.DefaultPage = spaPath+"/index.html";
        spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions{
            RequestPath = spaPath,
        };
        spa.Options.SourcePath = "ClientApp";
        if (env.IsDevelopment())
        {
            spa.UseReactDevelopmentServer(npmScript: "start");
        }
    });
});

Don't use app.UsePathBase(new PathString("/foo")) unless you understand you do want to override the path base for all routes.

这篇关于ASP.Net Core 3.0(3.1)为单页应用程序设置PathBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆