c# – 如何在Nancyfx中添加请求标头?

我尝试在ApplicationStartup覆盖中的引导程序中添加它.

pipelines.AfterRequest.AddItemToStartOfPipeline(ctx =>
{
  ctx.Request.Headers["x-fcr-version"] = "1";
});

它给了我错误.

谁能指出我正确的方向?

解决方法

请注意在尝试操作响应时如何尝试设置请求?

试试这个..

protected override void RequestStartup(TinyIoCContainer container,IPipelines pipelines,NancyContext context)
{
    base.RequestStartup(container,pipelines,context);

    pipelines.AfterRequest.AddItemToEndOfPipeline(c =>
    {
        c.Response.Headers["x-fcr-version"] = "1";
    });
}

这就是我的Response看起来像..

或者..如果要在模块级别设置它,可以使用Connection Negotiation

Get["/"] = parameters => {
    return Negotiate
        .WithModel(new RatPack {FirstName = "Nancy "})
        .WithMediarangeModel("text/html",new RatPack {FirstName = "Nancy fancy pants"})
        .WithView("negotiatedview")
        .WithHeader("X-Custom","SomeValue");
};

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...