Bri*_*din 1 c# asp.net asp.net-mvc asp.net-core
看起来这应该是一个简单的问题,但我一直无法通过谷歌找到解决方案。
IHttpHandler在 ASP.NET Core 中,实现者被中间件类取代,这似乎是相当标准的。旧系统的一个优点是您可以设置一个 HTTP 处理程序来响应 web.config 中指定的路由。
因此,例如,如果我的IHttpHandler实现者名为FooHandler,则 web.config 将包含类似以下内容的内容:
<location path="foo">
<system.webServer>
<handlers>
<add name="FooHandler" path="*" verb="*" type="FooCompany.FooProduct.FooHandler, FooCompany.FooProduct"/>
</handlers>
</system.webServer>
</location>
Run Code Online (Sandbox Code Playgroud)
ASP.NET Core 中是否有一对一的路由替代方案?我该怎么做呢?
编辑:新的中间件类可能看起来像:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
namespace FooCompany.FooProduct.Middleware
{
public class FooMiddleware
{
private readonly RequestDelegate _next;
public FooMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync("OK");
await _next.Invoke(context);
}
}
public static class FooMiddlewareExtensions
{
public static IApplicationBuilder UseFoo(this IApplicationBuilder builder)
{
return builder.UseMiddleware<FooMiddleware>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用 IApplicationBuilder 的 Map 扩展方法:
public static class FooMiddlewareExtensions
{
public static IApplicationBuilder UseFoo(this IApplicationBuilder builder, string path)
{
return builder.Map(path, b => b.UseMiddleware<FooMiddleware>());
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以在中间件中执行此操作
public class FooMiddleware
{
private readonly RequestDelegate _next;
private readonly PathString _path;
public FooMiddleware(RequestDelegate next, PathString path)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (!context.Request.Path.StartsWithSegments(path))
{
// jump to the next middleware
await _next.Invoke(context);
}
// do your stuff
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4006 次 |
| 最近记录: |