[英]ASP.NET OWIN Middleware - modify HTTP response
我需要拦截所有的aspx和js文件请求,并替换一些存在的文本标签。 这个中间件应该作为一个 IIS 模块工作,显然不会干扰 web 应用程序的正常流程。 我写了一些代码,但我不知道该怎么做。
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.Use(typeof(FilterMiddleware), Console.Out);
}
}
public class FilterMiddleware : OwinMiddleware
{
private readonly TextWriter logger;
private readonly OwinMiddleware nextMiddleware;
public FilterMiddleware(OwinMiddleware nextMiddleware, TextWriter logger) : base(nextMiddleware)
{
this.nextMiddleware = nextMiddleware;
this.logger = logger;
}
public override async Task Invoke(IOwinContext context)
{
var headers = context.Request.Headers;
// IF .js OR .ASPX REPLACE TEXT HERE //
await nextMiddleware.Invoke(context);
}
}
我想你正在寻找的是
if (context.Request.ContentType = "application/json") // or whatever MIME type
{
...
}
然后一旦你完成了所有的处理,不要忘记创建一个回复
context.Response.ContentType = "application/json";
string result = ""; // whatever string you are sending back
await context.Response.WriteAsync(result);
但是,如果它遇到某种错误,例如不受支持的方法(即 PUT)
context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
await context.Response.WriteAsync(String.Empty);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.