【问题标题】:ASP.NET OWIN Middleware - modify HTTP responseASP.NET OWIN 中间件 - 修改 HTTP 响应
【发布时间】:2019-01-14 19:15:13
【问题描述】:

我需要拦截所有 aspxjs 文件请求并替换一些文本标签存在。这个中间件应该作为一个 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);
    }
}

【问题讨论】:

    标签: c# asp.net owin


    【解决方案1】:

    我认为你正在寻找的是

    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);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2010-12-03
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    相关资源
    最近更新 更多