相关疑难解决方法(0)

为什么我的嵌套HttpModule EndRequest事件处理程序不会触发?

当我尝试EndRequest使用嵌套HttpModule在MVC 5.2.2和.NET 4.6.2上的事件处理程序修改标头时,我有一些奇怪的行为.如果我不在EndRequest我的顶级修改HttpModule,看起来嵌套中的事件处理程序HttpModule永远不会触发,即使我知道Init在嵌套上调用了HttpModule.

我的问题是,我的代码中发生了什么,以防止"TestNested"标题出现在响应头中,除非我包含添加了一个EndRequest什么都不做的事件处理程序的注释掉的代码?


动态注册我的顶级 HttpModule

[assembly: PreApplicationStartMethod(typeof(PreApplicationStartClass), "Start")]
namespace MyNamespace
{
    public class PreApplicationStartClass
    {
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(TopHttpModule));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从单个顶级模块调用Init我的所有其他HttpModules模块

namespace MyNamespace
{
    public class TopHttpModule: IHttpModule
    {
        private readonly Lazy<IEnumerable<IHttpModule>> _modules = 
            new Lazy<IEnumerable<IHttpModule>>(RetrieveModules);

        private static IEnumerable<IHttpModule> RetrieveModules()
        {
            return DependencyResolver.Current.GetServices<IHttpModule>();
        }

        public void Init(HttpApplication context)
        {
            var modules = _modules.Value;
            foreach (var …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc httpmodule

59
推荐指数
1
解决办法
3845
查看次数

为什么HttpCacheability.Private会抑制ETag?

在编写自定义IHttpHandler时,我遇到了一个我没想到的关于HttpCachePolicy对象的行为.

我的处理程序计算并设置一个实体标记(使用与当前响应对象关联的HttpCachePolicy上的SetETag方法).如果我使用SetCacheability方法将缓存控件设置为public,则所有内容都像魅力一样,服务器会沿着e-tag标头发送.如果我将其设置为私有,则将禁止e-tag标头.

也许我只是看起来不够努力,但我没有在HTTP/1.1规范中看到任何可以证明这种行为的理由.为什么你不想在仍禁止代理存储数据的同时向浏览器发送E-Tag?

using System;
using System.Web;

public class Handler : IHttpHandler {
    public void ProcessRequest (HttpContext ctx) {
        ctx.Response.Cache.SetCacheability(HttpCacheability.Private);
        ctx.Response.Cache.SetETag("\"static\"");
        ctx.Response.ContentType = "text/plain";
        ctx.Response.Write("Hello World");
    }

    public bool IsReusable { get { return true; } }
}
Run Code Online (Sandbox Code Playgroud)

将返回

Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 11

但如果我们将其改为公开,它将会回归

Cache-Control: public
Content-Type: text/plain; charset=utf-8
Content-Length: 11
Etag: "static"

到目前为止,我已经在ASP.NET开发服务器和IIS6上运行了相同的结果.此外,我无法使用明确设置ETag

Response.AppendHeader("ETag", "static")
Run Code Online (Sandbox Code Playgroud)

更新:在IIS7中运行时可以手动附加ETag标头,我怀疑这是由ASP.NET和IIS7管道之间的紧密集成引起的.

澄清:这是一个很长的问题,但核心问题是:为什么ASP.NET会这样做,我该如何解决它?我应该这样做吗?

更新:我将接受Tony的答案,因为它基本上是正确的(去Tony!).我发现如果你想完全模拟HttpCacheability.Private,你可以设置ServerAndPrivate的可缓存性,但你也有调用缓存.SetOmitVaryStar(true)否则缓存会将Vary:*标头添加到输出中,您不希望这样.当我获得编辑权限时,我会将其编辑到答案中(或者如果您看到这个Tony也许您可以编辑您的答案以包含该呼叫?)

c# asp.net caching http

25
推荐指数
1
解决办法
6759
查看次数

当MVC和Web API在不同的项目中时,如何存储持有者令牌

情况: 我有一个Web API 2项目,它充当授权服务器(/ token endpoint)和资源服务器.我正在使用ASP.Net Web API开箱即用的模板减去任何MVC参考.Start.Auth配置如下:

public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context and user manager to use a single instance per request
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Configure the application for OAuth based flow
            PublicClientId = …
Run Code Online (Sandbox Code Playgroud)

cookies asp.net-mvc oauth-2.0 asp.net-web-api bearer-token

10
推荐指数
1
解决办法
1万
查看次数