2017-11-08 6 views
0

私は、特定のURLが要求されたときにIISが基本的に何もしないようにするために、私がサーバサイドからレンダリングしたルータを反応させてリクエストを処理する必要があるからです。このlinkミドルウェアでのリクエストを無視する

を使用し

私は、各要求をチェックし、中央を作成しました。今私は右のURLを見つけると、この要求を無視するか、中止するかを知らない。

public class IgnoreRouteMiddleware 
{ 

    private readonly RequestDelegate next; 

    // You can inject a dependency here that gives you access 
    // to your ignored route configuration. 
    public IgnoreRouteMiddleware(RequestDelegate next) 
    { 
     this.next = next; 
    } 

    public async Task Invoke(HttpContext context) 
    { 
     if (context.Request.Path.HasValue && 
      context.Request.Path.Value!="/") 
     { 


      // cant stop anything here. Want to abort to ignore this request 

     } 

     await next.Invoke(context); 
    } 
} 

答えて

3

あなたが要求を停止する場合、これはパイプラインの次のミドルウェアを呼び出しますので、ちょうど、next.Invoke(context)を呼び出すことはありません。それを呼び出さずに、リクエストを終了します(そして、それ以前のミドルウェアのコードはnext.Invoke(context)の後に処理されます)。あなたのケースでは

は、単に他の支店に電話を移動したり、単に表現

public class IgnoreRouteMiddleware 
{ 

    private readonly RequestDelegate next; 

    // You can inject a dependency here that gives you access 
    // to your ignored route configuration. 
    public IgnoreRouteMiddleware(RequestDelegate next) 
    { 
     this.next = next; 
    } 

    public async Task Invoke(HttpContext context) 
    { 
     if (!(context.Request.Path.HasValue && context.Request.Path.Value!="/")) 
     { 
      await next.Invoke(context); 
     } 
    } 
} 

はまた、ミドルウェアがどのように働くかについてよりよく理解するためにASP.NET Core Middlewareドキュメントを読んでください場合は否定。

ミドルウェアは、要求と応答を処理するアプリケーションパイプラインに組み込まれたソフトウェアです。各コンポーネント:

  • 要求をパイプラインの次のコンポーネントに渡すかどうかを選択します。
  • パイプラインの次のコンポーネントが前後に実行できるかどうかは、です。

しかし、あなたは、サーバーサイドレンダリングをしたい場合は、すでに新しいテンプレート(ASP.NETコア2.0.x)の中に組み込まれているMicrosoft`s JavaScript/SpaServicesライブラリを、使用することを検討してのような代替ルートを登録。

app.UseMvc(routes => 
{ 
    routes.MapRoute(
     name: "default", 
     template: "{controller=Home}/{action=Index}/{id?}"); 

    routes.MapSpaFallbackRoute(
     name: "spa-fallback", 
     defaults: new { controller = "Home", action = "Index" }); 
}); 

新しいテンプレートは、ホットモジュール交換

のためのサポートが付属しています
関連する問題