.NET Core(2.1)Web API控制器接受请求URL中的所有后续内容作为参数 [英] .NET Core (2.1) web API controller accepting all that follows within the request url as parameter

查看:141
本文介绍了.NET Core(2.1)Web API控制器接受请求URL中的所有后续内容作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的是一个.NET Core 2.1 Web API控制器(在下面的TestController中),当接收到GET请求时,该控制器应该生成到其他URL的重定向。

What I have is a .NET Core 2.1 web API controller (in the following the TestController) that should generate redirects to other urls when receiving GET requests.

示例:

控制器的调用方式如下: http://localhost/api/v1/Test/somedir/somesubdir/filename.extension

The controller is called like: http://localhost/api/v1/Test/somedir/somesubdir/filename.extension

,它应该返回重定向到< a href = https://example-domain.com/somedir/somesubdir/filename.extension rel = nofollow noreferrer> https://example-domain.com/somedir/somesubdir/filename.extension

and it should return a redirect to https://example-domain.com/somedir/somesubdir/filename.extension

此问题的示例控制器如下:

My example controller for this question looks like:

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

我没有安排工作的路线。如果我使用Swagger调用该方法,则会被调用(用斜杠替换为%2F),但至少会被调用。
如果我通过邮递员.NET Core调用控制器,则仅返回404 Not Found。

I don't get the routing to work. If I call the method with Swagger it get's called (with the slashes replaced by %2F) but at least it gets called. If I call the controller via postman .NET Core just returns 404 Not Found.

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

I do not necessarily need the HttpGet("{path}"). I know that I could get the path like I assigned the path2 variable.

任何提示都可以正确实现路由?

Any hints how I could get the routing right?

另一个可能的解决方案:

Another possible solution:

正如John和Kirk Larkin在评论中指出的那样for是一条通俗易懂的路线,在路径参数中填充 somedir / somesubdir / filename.extension,与之后的路径无关。

As pointed out by John and Kirk Larkin in the comments what I was looking for is a catch-all route filling the path argument with "somedir/somesubdir/filename.extension" independent how long the path afterwards is.

[HttpGet( {* path})]

[HttpGet("{*path}")]

推荐答案

您不需要考虑 api / v1 / Test ,因为您的代码注释表明,它已经被过滤掉了。控制器级别的[Route]属性。

You don't need to take "api/v1/Test" into account as your code comments suggest, it is already filtered out by the [Route] attribute on the Controller level.

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

For the rest of the path that follows you can use {*path}:

[HttpGet("{*path}")]
public async Task<IActionResult> Get(string path)
{
    const string prefix = "https://example-domain.com/api/v1/Other/";
    return Redirect(prefix + path);
}

这篇关于.NET Core(2.1)Web API控制器接受请求URL中的所有后续内容作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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