【发布时间】:2017-12-14 23:28:30
【问题描述】:
似乎这应该是一个简单的问题,但我一直无法通过 Google 找到解决方案。
在 ASP.NET Core 中,IHttpHandler 实现者被中间件类取代似乎很标准。旧系统的一个优点是您可以设置一个 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>
在 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>();
}
}
}
【问题讨论】:
-
可以像
builder.Map("/path", b => b.UseMiddleware<FooMiddleware>())一样使用IApplicationBuilder的地图扩展方法
标签: c# asp.net asp.net-mvc asp.net-core