忽略中间件中的请求
Ignoring Request in middleware
所以我希望 IIS 在请求某些 URL 时基本上不做任何事情,因为我想要从服务器端呈现到的 React 路由器来处理请求。
用过这个
我创建了一个检查每个请求的中间件。现在我不知道如何在找到正确的网址后忽略或中止此请求。
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);
}
}
如果要停止请求,请不要调用 next.Invoke(context)
,因为这将调用管道中的下一个中间件。不调用它,只是结束请求(并且将处理next.Invoke(context)
之后的先前中间件代码)。
在你的情况下,只需将调用移至 else 分支或否定 if 表达式
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 文档以更好地了解中间件的工作原理。
Middleware is software that is assembled into an application pipeline to handle requests and responses. Each component:
- Chooses whether to pass the request to the next component in the pipeline.
- Can perform work before and after the next component in the pipeline is invoked.
但如果您想要服务器端呈现,请考虑使用 Microsoft 的 JavaScript/SpaServices 库,该库已内置在较新的模板中 (ASP.NET Core 2.0.x)并注册后备路线,例如。
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
新模板还支持模块热替换
所以我希望 IIS 在请求某些 URL 时基本上不做任何事情,因为我想要从服务器端呈现到的 React 路由器来处理请求。
用过这个
我创建了一个检查每个请求的中间件。现在我不知道如何在找到正确的网址后忽略或中止此请求。
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);
}
}
如果要停止请求,请不要调用 next.Invoke(context)
,因为这将调用管道中的下一个中间件。不调用它,只是结束请求(并且将处理next.Invoke(context)
之后的先前中间件代码)。
在你的情况下,只需将调用移至 else 分支或否定 if 表达式
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 文档以更好地了解中间件的工作原理。
Middleware is software that is assembled into an application pipeline to handle requests and responses. Each component:
- Chooses whether to pass the request to the next component in the pipeline.
- Can perform work before and after the next component in the pipeline is invoked.
但如果您想要服务器端呈现,请考虑使用 Microsoft 的 JavaScript/SpaServices 库,该库已内置在较新的模板中 (ASP.NET Core 2.0.x)并注册后备路线,例如。
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
新模板还支持模块热替换