繁体   English   中英

如何将数据从ASP.NET WebAPI ApiController传递到ASP.NET MVC控制器?

[英]How do I pass data from an ASP.NET WebAPI ApiController to an ASP.NET MVC Controller?

假设我在ASP.NET Web API控制器中具有以下方法:

public HttpResponseMessage Get(string code1, string code2)
{
    var response = new HttpResponseMessage(HttpStatusCode.Redirect);
    response.Headers.Location = new Uri("/", UriKind.Relative);

    return response;
}

我想接受传入的代码1和代码2的值,并将它们提供给MVC控制器(基于上面的重定向,我们假设它是HomeController)。 做这个的最好方式是什么?

一些注意事项:

  • 我没有打电话给WebAPI。 第三方站点调用端点并传递代码1和代码2(这是OAuth舞蹈的一部分,这是我使用重定向的原因)。
  • 值之一是稍后用于身份验证的访问令牌。 (是的,这最终将使用HTTPS。)
  • 我想要一种足够灵活的方法,以将一个或多个值传递回MVC控制器。
  • 我不一定需要数据才能通过会话。

谢谢!

更新:这是最终代码和方法的外观。

public async Task<HttpResponseMessage> Get(string display, string code)
{
    var auth = new AuthenticationClient();
    await auth.WebServer(_consumerKey, _consumerSecret, _callbackUrl, code);

    var url = string.Format("/?token={0}&api={1}&instance_url={2}", auth.AccessToken, auth.ApiVersion,
        auth.InstanceUrl);

    var response = new HttpResponseMessage(HttpStatusCode.Redirect);
    response.Headers.Location = new Uri(url, UriKind.Relative);

    return response;
}

除了使用查询字符串参数外,不确定是否还有其他选择。 路径参数实际上没有多大意义,因为没有带有身份验证令牌的自然层次结构。

暂无
暂无

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

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