简体   繁体   English

在 Web Api.Net 中路由重定向 URL

[英]Routing a redirect URL in Web Api .Net

I am working on Github api Oauth.我正在研究 Github api Oauth。 The main problem is to match callback URl with method in Web Api主要问题是将回调 URl 与 Web Api 中的方法匹配

     [HttpGet]
    [Route("api/values/callback/{code}&{state}")]
    public JsonResult Get (string code, string state)
    {
        var s = User.Claims;
        return new JsonResult(s);
    }

In StartUp file: options.CallbackPath = new PathString("/api/values/callback/");在启动文件中: options.CallbackPath = new PathString("/api/values/callback/"); Redirect in URL that should match an action in service http://localhost:5000/api/values/callback/?code=b1b0659f6e0&state=CfDJ8Ir-xT So, I can not build rout which has pattern /?param1&param2 Here is redirect URL that should match: Redirect in URL that should match an action in service http://localhost:5000/api/values/callback/?code=b1b0659f6e0&state=CfDJ8Ir-xT So, I can not build rout which has pattern /?param1&param2 Here is redirect URL that应该匹配:

Would be glad for help:)很高兴得到帮助:)

You can use [FromQuery] to get values from the query string.您可以使用[FromQuery]从查询字符串中获取值。

    [HttpGet] 
    [Route("api/values/callback")]
    public JsonResult Get([FromQuery]string code, string state)
    {
        string s = "code:" + code + "         state:" + state;
        return new JsonResult(s);
    }

Test测试

在此处输入图像描述

If you're using OidClient you can use the following to inject the service to the method and get the querystring to process.如果您使用的是 OidClient,则可以使用以下内容将服务注入该方法并获取要处理的查询字符串。


// route method here
public async Task<ActionResult> ProcessCallback([FromServices] HttpListenerRequest request){
...

// generate the options using `OidcClientOptions`
 var client = new OidcClient(options);
        var result = await client.ProcessResponseAsync(request.QueryString.ToString(), null);
...

}

I would suggest to edit the route to api/values/callback/code={code}&state={state}.我建议编辑到 api/values/callback/code={code}&state={state} 的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM