C# .NET Core(2.1)web API控制器接受请求url中的所有内容作为参数

C# .NET Core(2.1)web API控制器接受请求url中的所有内容作为参数,c#,.net-core,asp.net-core-routing,.net-core-2.1,C#,.net Core,Asp.net Core Routing,.net Core 2.1,我拥有的是一个.NETCore2.1WebAPI控制器(在下面的TestController中),当接收GET请求时,它应该生成指向其他URL的重定向 例如: 控制器的名称如下所示: 它应该返回一个重定向到 这个问题的示例控制器如下所示: [Authorize] [Route("api/v1/[controller]")] public class TestController : ControllerBase { [HttpGet("{path}")] publ

我拥有的是一个.NETCore2.1WebAPI控制器(在下面的TestController中),当接收GET请求时,它应该生成指向其他URL的重定向

例如:

控制器的名称如下所示:

它应该返回一个重定向到

这个问题的示例控制器如下所示:

  [Authorize]
  [Route("api/v1/[controller]")]
  public class TestController : ControllerBase
  {
    [HttpGet("{path}")]
    public async Task<IActionResult> Get(string path)
    {
      //path e.g. is somedir/somesubdir/filename.extension
      string prefix = "https://example-domain.com/api/v1/Other/";
      //string path2 = HttpContext.Request.Path.Value.Replace("/api/v1/Test/", "/api/v1/Other/").Replace("%2F", "/");
      return Redirect(prefix + path);
    }
  }
[授权]
[路由(“api/v1/[控制器]”)]
公共类TestController:ControllerBase
{
[HttpGet(“{path}”)]
公共异步任务获取(字符串路径)
{
//路径,例如是somedir/somesubdir/filename.extension
字符串前缀=”https://example-domain.com/api/v1/Other/";
//字符串path2=HttpContext.Request.Path.Value.Replace(“/api/v1/Test/”、“/api/v1/Other/”)。Replace(“%2F”、“/”);
返回重定向(前缀+路径);
}
}
我无法让路线图正常工作。如果我使用Swagger调用该方法,则会调用它(斜杠替换为%2F),但至少会调用它。 如果我通过postman.NET内核调用控制器,则返回404 Not Found

我不一定需要HttpGet(“{path}”)。我知道我可以像分配path2变量一样获得路径

有没有什么提示可以告诉我如何正确选择路线


另一种可能的解决办法:

正如John和Kirk Larkin在评论中指出的那样,我所寻找的是一个包罗万象的路由,在path参数中填充“somedir/somesubdir/filename.extension”,与路径的长度无关

只需在变量名前面加一个星号就可以了


[HttpGet(“{*path}”)]

我认为您需要接收URL中分开的3个参数,因此。。这个方法应该是这样的

[Route("{dir}/{subdir}/filename")]
public async Task<IActionResult> Get(string dir, string subdir, string filename)
{
  string path = dir + "/" + subdir + "/" + filename;
  //path e.g. is somedir/somesubdir/filename.extension
  string prefix = "https://example-domain.com/api/v1/Other/";
  //string path2 = HttpContext.Request.Path.Value.Replace("/api/v1/Test/", "/api/v1/Other/").Replace("%2F", "/");
  return Redirect(prefix + path);
}
[路由(“{dir}/{subdir}/filename”)]
公共异步任务Get(字符串目录、字符串子目录、字符串文件名)
{
字符串路径=目录+“/”+子目录+“/”+文件名;
//路径,例如是somedir/somesubdir/filename.extension
字符串前缀=”https://example-domain.com/api/v1/Other/";
//字符串path2=HttpContext.Request.Path.Value.Replace(“/api/v1/Test/”、“/api/v1/Other/”)。Replace(“%2F”、“/”);
返回重定向(前缀+路径);
}

@john,他的解决方案很好:
[HttpGet(“{*path}”)]
,刚刚测试过。但我想保留我的答案作为功能选项:

作为另一种选择,您可以遵循MSDN[全包路线]:


您不需要考虑
“api/v1/Test”
,正如您的代码注释所示,它已经被控制器级别的[Route]属性过滤掉了

对于后续路径的其余部分,您可以使用
{*path}

[HttpGet("{*path}")]
public async Task<IActionResult> Get(string path)
{
    const string prefix = "https://example-domain.com/api/v1/Other/";
    return Redirect(prefix + path);
}
[HttpGet(“{*path}”)]
公共异步任务获取(字符串路径)
{
常量字符串前缀=”https://example-domain.com/api/v1/Other/";
返回重定向(前缀+路径);
}

请不要引用我的话:)但我发誓你可以做一些类似于
{*path}
@john post你的评论作为答案,我会接受的,它的工作原理与HttpGet({*path})完全一样,如果这有帮助的话,它被称为“一网打尽”。对不起,我后来马上就睡着了。我很高兴能帮上忙。不幸的是,我不能这样做,因为我不知道路径的传入结构。它可以是somedir/somesubdir/file.png或文件名之前的15个子目录,然后您需要在一个参数中接收所有路径,并按照Swagger的方法对url的路径段进行编码
public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.Map("api/v1/Test", x =>
        {
            x.Run(async context =>
            {
                //path e.g. is somedir/somesubdir/filename.extension
                string prefix = "https://example-domain.com/api/v1/Other/";
                string path = context.Request.Path.Value.Replace("/api/v1/Test/", "/api/v1/Other/").Replace("%2F", "/");
                context.Response.Redirect(prefix + path);
            });
        });
    }
}
[HttpGet("{*path}")]
public async Task<IActionResult> Get(string path)
{
    const string prefix = "https://example-domain.com/api/v1/Other/";
    return Redirect(prefix + path);
}