Asp.net网页API重定向请求 [英] Asp.net Web Api Redirect request

查看:442
本文介绍了Asp.net网页API重定向请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪几种CRUD方法的Asp.net的Web API项目。

I have an Asp.net Web API project which has several CRUD methods.

在这些方法之上,我想添加一个授权服务,读取访问资源(如果他们没有被授权的授权报头和prevent用户)。

On top of these methods, i want to add an Authorization service that reads the Authorization header and prevent users of accessing the resources (if they are not authorized).

// Method on internal IP Project
public class InternalController : ApiController
{
    public void Create(CreateRequest request)
    {
        // implement the method
    }
}

// Method on public IP Project
public class ExternalController : ApiController
{
    public async Task Create(CreateRequest request)
    {
        // validate Authorization header and throw exception if not valid

        using (HttpClient client = new HttpClient())
        {
            string parameters = string.Format("param1={0}&param2={1}", request.Param1, request.Param2);

            client.BaseAddress = new Uri("http://192.168.1.1/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("api/internal/create?" + parameters);
            response.EnsureSuccessStatusCode();
        }
    }
}

有重定向,从外部API的内部API请求更容易?

Is there any way of "Redirecting" the request from the External API to the Internal API more easily?

现在,我必须手动重新创建所有这些我都接受ExternalAPI并在InternalAPI发送它们的参数,即使它们是一样的。

Right now, i have to manually re-create all the parameters that i receive in ExternalAPI and send them in the InternalAPI, even if they are the same.

我可以让的HttpClient 自动发送,我有ExternalAPI的的Htt prequestMessage(请求)对象方法是什么?

Can i make HttpClient automatically send the HttpRequestMessage (Request) object that i have in ExternalAPI method?

推荐答案

当谈到的ASP.NET Web API 。 HttpClient的不会自动跳转。当你已经成为从内部服务的响应可以将其传递给外部。或者,你可以重定向你的动作如这里

When speaking about ASP.NET Web API. HttpClient will not automatically redirect you. When you have become response from internal service you can pass it to external. Or you can redirect your action like here

要使它正确重定向客户端从视图使用HTTP重定向页眉和repsonse codeS的REST点。例如 HTTP响应code 302 。然后,客户端应该能够在这样的反应code发生反应,并从Location头重定向地址。但是,它是关于重定向客户端。

To make it correct redirection for client from REST point of view use HTTP Redirect Headers and repsonse codes. For example HTTP response code 302. And then client should be able to react on such response code and get redirect address from Location header. But it's about redirect for client.

当您的API从建筑谈论内部的一些业务的呼叫即可。您有以下选择:

When speaking about call of some internal services from your API from architecture. You have following alternatives:


  1. 呼叫您的内部服务作为类方法

  2. 请服务,服务电话

  3. Setup消息队列或公交车,你的API将通过服务总线与它通信。

拨打您的内部服务作为类方法
好简单。没有影响,延误服务调用。但你应该参考组装和它并不总是可能的。或者,这样的方式可能是不可能的,因为要求

Call your internal service as class method Very easy. No impact and delays for service call. But you should reference assembly and it's not always possible. Or such way could be not possible due to requirements

制作服务,服务电话
也有缺点:你的服务是紧密耦合的,你有延迟,应等待来自内部的服务响应。它认为是不好的做法。但可能是一个很好的解决方案,暂时作为第一步,服务总线。

Make service to service call Has disadvantages: your services are tightly coupled, you have delay and should wait for response from internal service. It's considered as bad practice. But could be a good temporarily solution as first step to service bus.

Setup消息队列或公交车,你的API将通过服务总线与它通信。
你的服务是分离的,独立的。你不应该等待回应。但它在技术上难以建立,使你的架构和基础设施更加复杂/

Setup message queue or bus and your API will communicate with it through service bus. Your services are decoupled and independent. You shouldn't wait for response. But it's technically harder to set up and make your architecture and infrastructure more complex/

由于摘要
没有最好的方式从包装盒为你的架构,你应该从替代品根据自己的需要进行选择。

As summary There is no best way from the box for your architecture and you should select from alternatives based on your requirements.

这篇关于Asp.net网页API重定向请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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