【问题标题】:ASP.NET Action Execute with PathBase使用 PathBase 执行 ASP.NET 操作
【发布时间】:2021-02-03 23:15:18
【问题描述】:

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

HomeController 动作:

public class HomeController : Controller
  {
  public HomeController(StatelessServiceContext context){
  }
  public IActionResult UsernameAuthentication()
    {
      return View();
    }
  }

动作执行:

http://localhost:9040/Home/UserNameAuthentication

操作未执行:

http://localhost:9040/Application1/Home/UserNameAuthentication

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

谢谢。

【问题讨论】:

  • 你能用app.UsePathBase("/Application1");而不是app.Use((context, next) => { context.Request.PathBase = new PathString("/Application1"); return next.Invoke(); });试试吗?我的情况是这样
  • @r08 有什么更新吗?我的回复对你有帮助吗?

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

据我所知,如果您使用 Application1/Home/UserNameAuthentication 作为传递给应用程序的 url,应用程序 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();
            }
        });

结果:

【讨论】:

    猜你喜欢
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2012-12-12
    相关资源
    最近更新 更多