简体   繁体   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?

Let's say I have the following method in my ASP.NET Web API 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;
}

I'd like to take the values passed in for code1 and code2 and make them available to an MVC Controller (based on the redirect above, let's just assume it's the HomeController). 我想接受传入的代码1和代码2的值,并将它们提供给MVC控制器(基于上面的重定向,我们假设它是HomeController)。 What is the best way to do this? 做这个的最好方式是什么?

A few considerations: 一些注意事项:

  • I'm not calling the WebAPI. 我没有打电话给WebAPI。 A 3rd party site calls the endpoint and passes along code1 and code2 (it's part of an OAuth dance and is the reason I use the redirect). 第三方站点调用端点并传递代码1和代码2(这是OAuth舞蹈的一部分,这是我使用重定向的原因)。
  • One of the values will be an access token used later for authentication. 值之一是稍后用于身份验证的访问令牌。 (Yes, this would eventually use HTTPS.) (是的,这最终将使用HTTPS。)
  • I'd like a method that's flexible enough to pass one or more values back to the MVC Controller. 我想要一种足够灵活的方法,以将一个或多个值传递回MVC控制器。
  • I do not necessarily need the data to live passed the session. 我不一定需要数据才能通过会话。

Thanks! 谢谢!

Update: Here's a look at the final code and approach. 更新:这是最终代码和方法的外观。

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

Not sure you have much choice other than to use a query string parameter. 除了使用查询字符串参数外,不确定是否还有其他选择。 Path parameters don't really make much sense as there is no natural hierarchy with an auth token. 路径参数实际上没有多大意义,因为没有带有身份验证令牌的自然层次结构。

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

相关问题 Ninject与ASP.NET MVC 5控制器和apicontroller - Ninject with asp.net mvc 5 controller and apicontroller 如何将数据从控制器传递给asp.net mvc中的强类型用户控件? - How do I pass data from a controller to a strongly typed user control in asp.net mvc? 如何将 ID 传递给 ASP.NET MVC 中的控制器? - How do I pass an ID to the controller in ASP.NET MVC? 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 如何将数据从 Controller 传递到 ASP.NET MVC 中的 WebForm? - How to pass data from Controller to WebForm in ASP.NET MVC? WebApi(ApiController)与ASP.Net MVC中的OData(ODataController) - WebApi (ApiController) vs OData (ODataController) in ASP.Net MVC 如何将DateTime参数从View传递到ASP.net MVC5中的Controller? - How do I pass DateTime parameters from View to Controller in ASP.net MVC5? ASP.NET MVC-如何在Webapi控制器返回的Razor视图中显示数据 - ASP.NET MVC - How to display data in Razor view returned from Webapi controller 将数据从ASp.NET MVC Controller传递到JavaScript函数 - Pass data from ASp.NET MVC Controller to JavaScript function 无法将数据从视图传递到 controller ASP.NET MVC 5 - Unable to pass data from view to controller ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM