C# MVC 6-Krestel在构建request.path时抛出错误,将调用路由到控制器的任何变通方法
我收到以下异常:C# MVC 6-Krestel在构建request.path时抛出错误,将调用路由到控制器的任何变通方法,c#,asp.net-core,asp.net-core-mvc,kestrel-http-server,C#,Asp.net Core,Asp.net Core Mvc,Kestrel Http Server,我收到以下异常: System.ArgumentException at Microsoft.AspNet.Http.PathString..ctor(String value) at Microsoft.AspNet.Http.Internal.DefaultHttpRequest.get_Path() at Microsoft.AspNet.StaticFiles.Helpers.TryMatchPath(HttpContext context, PathString
System.ArgumentException
at Microsoft.AspNet.Http.PathString..ctor(String value)
at Microsoft.AspNet.Http.Internal.DefaultHttpRequest.get_Path()
at Microsoft.AspNet.StaticFiles.Helpers.TryMatchPath(HttpContext context, PathString matchUrl, Boolean forDirectory, PathString& subpath)
the Request METHOD is GET
HTTP Version HTTP/1.1
Content-Type : application/x-www-form-urlencoded
Accept-Encoding : {gzip}
任何帮助都将不胜感激
公共启动(IHostingEnvironment环境)
{
//设置配置源。
var builder=new ConfigurationBuilder()
.AddJsonFile(“appsettings.json”)
.AddJsonFile(“XDCRInfo.json”)
.AddenEnvironmentVariables();
Configuration=builder.Build();
}
public void配置服务(IServiceCollection服务)
{
var policy=new Microsoft.AspNet.Cors.Infrastructure.CorsPolicy();
policy.Headers.Add(“*”);
policy.Methods.Add(“*”);
policy.Origins.Add(“*”);
policy.SupportsCredentials=true;
services.Configure(Configuration.GetSection(“XDCRSettings”);
//添加Cors
services.AddCors(选项=>
{
选项。添加策略(“交叉源”,策略);
});
//添加框架服务。
services.AddMvc();
配置(选项=>
{
options.reserverBrowserAcceptHeader=true;
});
}
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
app.UseExceptionHandler(“/Home/Error”);
使用(下一步=>异步上下文=>
//将原始流保存在单独的文件中
//变量,以便在以后必要时恢复它。
var stream=context.Request.Body;
//优化:如果需要,则不缓冲请求
//没有溪流,也没有可倒带的溪流。
if(stream==stream.Null | | stream.CanSeek)
{
等待下一个(上下文);
返回;
}
尝试
{
使用(var buffer=new MemoryStream())
{
//将请求流复制到内存流。
等待流。CopyToAsync(缓冲区);
//倒带内存流。
缓冲器位置=0L;
//用内存流替换请求流。
context.Request.Body=缓冲区;
//调用管道的其余部分。
等待下一个(上下文);
}
}
捕获(System.ArgumentException ex)
{
var Frame=(Microsoft.AspNet.Server.Kestrel.Http.Frame)context.Features;
Console.WriteLine(Frame.RequestUri);
}
最后
{
//恢复原始流。
context.Request.Body=流;
Console.WriteLine(context.Request);
}
});
附录UseCors(“交叉来源”);
app.UseMvc();
}
App.use
用于捕获错误。否则,我无法查看请求。我已经找到了答案。如果绝对URL在路径中,红隼将断开。为了让它发挥作用,这是一个解决办法
app.Use(next => async context => {
// get the frame from kestrel and then but the path by removing the hostname
var Frame = (Microsoft.AspNet.Server.Kestrel.Http.Frame)context.Features;
var newpath = Frame.RequestUri.Replace("http://" + context.Request.Host.Value, "");
context.Request.Path = newpath;
// Invoke the rest of the pipeline.
await next(context);
});
至少向我们展示您的code.public Startup(IHostingEnvironment env){//设置配置源。var builder=new ConfigurationBuilder().AddJsonFile(“appsettings.json”).AddJsonFile(“XDCRInfo.json”).AddEnvironmentVariables();配置=builder.Build();}