C# 使用PathBase执行ASP.NET操作

C# 使用PathBase执行ASP.NET操作,c#,asp.net,asp.net-mvc,asp.net-core,C#,Asp.net,Asp.net Mvc,Asp.net Core,我有Asp.net核心(在.net框架上运行)web mvc应用程序 Startup.cs: public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets calle

我有Asp.net核心(在.net框架上运行)web mvc应用程序

Startup.cs:

public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
      {
        EnableQuickPulseMetricStream = true
      };
      services.AddMvc();
      services.AddApplicationInsightsTelemetry(aiOptions);
      services.AddCors(option =>
      {
        option.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("*"));
        option.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
      });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      TelemetryClient tc = new TelemetryClient();
      tc.TrackTrace("Environment: " + env);

      if (env.IsDevelopment())
      {
        tc.TrackTrace("Development");
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseDeveloperExceptionPage();
      }
      app.Use((context, next) =>
      {
        context.Request.PathBase = new PathString("/Application1");
        return next.Invoke();
      });
      app.UseStaticFiles();
      app.UseCors(select => select.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
      
      app.UseMvc(routes =>
      {
        routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");

        routes.MapSpaFallbackRoute(
                  name: "spa-fallback",
                  defaults: new { controller = "Home", action = "Index" });
      });
    }
  }
public class HomeController : Controller
  {
  public HomeController(StatelessServiceContext context){
  }
  public IActionResult UsernameAuthentication()
    {
      return View();
    }
  }
HomeController操作:

public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
      {
        EnableQuickPulseMetricStream = true
      };
      services.AddMvc();
      services.AddApplicationInsightsTelemetry(aiOptions);
      services.AddCors(option =>
      {
        option.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("*"));
        option.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
      });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      TelemetryClient tc = new TelemetryClient();
      tc.TrackTrace("Environment: " + env);

      if (env.IsDevelopment())
      {
        tc.TrackTrace("Development");
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseDeveloperExceptionPage();
      }
      app.Use((context, next) =>
      {
        context.Request.PathBase = new PathString("/Application1");
        return next.Invoke();
      });
      app.UseStaticFiles();
      app.UseCors(select => select.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
      
      app.UseMvc(routes =>
      {
        routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");

        routes.MapSpaFallbackRoute(
                  name: "spa-fallback",
                  defaults: new { controller = "Home", action = "Index" });
      });
    }
  }
public class HomeController : Controller
  {
  public HomeController(StatelessServiceContext context){
  }
  public IActionResult UsernameAuthentication()
    {
      return View();
    }
  }
正在执行的操作:

public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
      {
        EnableQuickPulseMetricStream = true
      };
      services.AddMvc();
      services.AddApplicationInsightsTelemetry(aiOptions);
      services.AddCors(option =>
      {
        option.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("*"));
        option.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
      });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      TelemetryClient tc = new TelemetryClient();
      tc.TrackTrace("Environment: " + env);

      if (env.IsDevelopment())
      {
        tc.TrackTrace("Development");
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseDeveloperExceptionPage();
      }
      app.Use((context, next) =>
      {
        context.Request.PathBase = new PathString("/Application1");
        return next.Invoke();
      });
      app.UseStaticFiles();
      app.UseCors(select => select.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
      
      app.UseMvc(routes =>
      {
        routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");

        routes.MapSpaFallbackRoute(
                  name: "spa-fallback",
                  defaults: new { controller = "Home", action = "Index" });
      });
    }
  }
public class HomeController : Controller
  {
  public HomeController(StatelessServiceContext context){
  }
  public IActionResult UsernameAuthentication()
    {
      return View();
    }
  }
http://localhost:9040/Home/UserNameAuthentication

操作未执行:

public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
      {
        EnableQuickPulseMetricStream = true
      };
      services.AddMvc();
      services.AddApplicationInsightsTelemetry(aiOptions);
      services.AddCors(option =>
      {
        option.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("*"));
        option.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
      });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      TelemetryClient tc = new TelemetryClient();
      tc.TrackTrace("Environment: " + env);

      if (env.IsDevelopment())
      {
        tc.TrackTrace("Development");
        app.UseDeveloperExceptionPage();
      }
      else
      {
        app.UseDeveloperExceptionPage();
      }
      app.Use((context, next) =>
      {
        context.Request.PathBase = new PathString("/Application1");
        return next.Invoke();
      });
      app.UseStaticFiles();
      app.UseCors(select => select.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
      
      app.UseMvc(routes =>
      {
        routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");

        routes.MapSpaFallbackRoute(
                  name: "spa-fallback",
                  defaults: new { controller = "Home", action = "Index" });
      });
    }
  }
public class HomeController : Controller
  {
  public HomeController(StatelessServiceContext context){
  }
  public IActionResult UsernameAuthentication()
    {
      return View();
    }
  }
http://localhost:9040/Application1/Home/UserNameAuthentication

我必须执行的任何其他配置,以使用PathBase执行操作?或使用上下文路径执行操作的任何其他方式。如何执行


谢谢。

据我所知,如果使用
Application1/Home/UserNameAuthentication
作为传递给应用程序的url,则application context.Request.Path将是
Application1/Home/UserNameAuthentication
,然后添加路径库,它将是
Application1/Application1/Home/UserNameAuthentication
这就是您得到404错误的原因

要解决此问题,应检查此Microsoft.AspNetCore.Http.PathString的开头是否与指定的Microsoft.AspNetCore.Http.PathString匹配

更多详细信息,请参考以下代码:

        app.Use((context, next) =>
        {
            string _pathBase = "/Application1";
            PathString matchedPath;
            PathString remainingPath;

            if (context.Request.Path.StartsWithSegments(_pathBase, out matchedPath, out remainingPath))
            {
                var originalPath = context.Request.Path;
                var originalPathBase = context.Request.PathBase;
                context.Request.Path = remainingPath;
                context.Request.PathBase = originalPathBase.Add(matchedPath);
                var re = context.Request.PathBase;
                return next.Invoke();

            }
            else
            {
                return next.Invoke();
            }
        });
结果:


你能用
app.UsePathBase(“/Application1”)试试吗
而不是
app.Use((context,next)=>{context.Request.PathBase=new PathString(“/Application1”);返回next.Invoke();})它在我的系统中工作case@r08有更新吗?我的回答对你有帮助吗?