Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 适用于所有流量的WebApi自定义路由_C#_Asp.net Web Api_Routing_Routeconfig - Fatal编程技术网

C# 适用于所有流量的WebApi自定义路由

C# 适用于所有流量的WebApi自定义路由,c#,asp.net-web-api,routing,routeconfig,C#,Asp.net Web Api,Routing,Routeconfig,我一直在寻找这个问题的答案,但运气不好,所以这里可能有人有更深刻的见解: 1) 我有一个进行http调用的应用程序。(在方框1上) 2) 我有访问数据库等的服务。(第二栏) 3) 我正在研究将位于另一个位置的服务,其主要目的是捕获来自框1的所有服务请求,并从框2重新生成服务调用,然后将结果返回框1。(住在3号包厢的中间人) 框1对框3进行http调用,框3调用框2,框3然后将结果返回框1 我有使用ExecuteAsync拦截请求的代码设置。我遇到的问题是,在appservice(框3)中,我无法

我一直在寻找这个问题的答案,但运气不好,所以这里可能有人有更深刻的见解:

1) 我有一个进行http调用的应用程序。(在方框1上)

2) 我有访问数据库等的服务。(第二栏)

3) 我正在研究将位于另一个位置的服务,其主要目的是捕获来自框1的所有服务请求,并从框2重新生成服务调用,然后将结果返回框1。(住在3号包厢的中间人)

框1对框3进行http调用,框3调用框2,框3然后将结果返回框1

我有使用
ExecuteAsync
拦截请求的代码设置。我遇到的问题是,在appservice(框3)中,我无法拦截调用,而不中断框2上存在的请求函数/路由(如果我没有,则返回404,因为框3上还不存在路由)

我的最终问题是:是否可以允许所有请求通过Web服务并点击
ExecuteAsync
函数,而不定义路由/函数

我尝试了RouteConfig中的
RegisterRoutes
函数的多种变体,但似乎没有任何效果

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "*",
        defaults: new { controller = "BaseController", action = "Index", id = UrlParameter.Optional }
    );
}

好吧,我知道了

实际上,我只需要删除1个http请求,如下所示:

[Route("api/{*url}")]
[HttpGet]
public IHttpActionResult Get() {
    return BadRequest();
}
除非ExecuteAsync函数失败,否则该函数实际上不会被命中。ExecuteAsync位于同一控制器中,如下所示:

public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
    {
        if (Properties.Settings.Default.Redirect)
        {
            var url = controllerContext.Request.RequestUri;
            url = new Uri(url.AbsoluteUri.Replace(
              Properties.Settings.Default.OriginalUriFragment,
              Properties.Settings.Default.ReplacemenUriFragment));
            var client = new HttpClient();
            client.DefaultRequestHeaders.Clear();
            foreach (var httpRequestHeader in controllerContext.Request.Headers)
            {
                client.DefaultRequestHeaders.Add(httpRequestHeader.Key, httpRequestHeader.Value);
            }
            if (controllerContext.Request.Method == HttpMethod.Get)
            {
                return client.GetAsync(url, cancellationToken);
            }
            if (controllerContext.Request.Method == HttpMethod.Post)
            {
                return client.PostAsync(url, controllerContext.Request.Content, cancellationToken);
            }
            if (controllerContext.Request.Method == HttpMethod.Delete)
            {
                return client.DeleteAsync(url, cancellationToken);
            }
            if (controllerContext.Request.Method == HttpMethod.Put)
            {
                return client.PutAsync(url, controllerContext.Request.Content, cancellationToken);
            }
        }

        return base.ExecuteAsync(controllerContext, cancellationToken);
    }
public override Task ExecuteAsync(HttpControllerContext controllerContext,CancellationToken CancellationToken)
{
if(Properties.Settings.Default.Redirect)
{
var url=controllerContext.Request.RequestUri;
url=新Uri(url.AbsoluteUri.Replace(
Properties.Settings.Default.OriginalUriFragment,
Properties.Settings.Default.ReplacemenUriFragment);
var client=新的HttpClient();
client.DefaultRequestHeaders.Clear();
foreach(controllerContext.Request.Headers中的var httpRequestHeader)
{
添加(httpRequestHeader.Key,httpRequestHeader.Value);
}
if(controllerContext.Request.Method==HttpMethod.Get)
{
返回client.GetAsync(url,cancellationToken);
}
if(controllerContext.Request.Method==HttpMethod.Post)
{
返回client.PostAsync(url、controllerContext.Request.Content、cancellationToken);
}
if(controllerContext.Request.Method==HttpMethod.Delete)
{
返回client.deleteAync(url,cancellationToken);
}
if(controllerContext.Request.Method==HttpMethod.Put)
{
返回client.PutAsync(url、controllerContext.Request.Content、cancellationToken);
}
}
返回base.ExecuteAsync(controllerContext,cancellationToken);
}