我为我们的站点编写了一个HttpModule,它通常接受请求并检查特定的文件扩展名以及特定会话变量的值.是否可以检测会话中的第一个请求?
有一个HttpModule可以更改响应标头中的服务器字段.但它在ASP.NET/IIS7经典模式下不起作用.在reponse标题中删除或更改服务器字段的解决方案是什么?
public class CloakHttpHeaderModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
}
public void Dispose()
{
}
private void context_PreSendRequestHeaders(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
context.Response.Headers.Set("Server", "Apache 2.0");
//HttpContext.Current.Response.Headers.Set("Server", "WSGIServer/0.1 Python/2.6.1");
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个MVC5应用程序,当在控制器上使用该属性时,它会在生产服务器上抛出NullReferenceException [Authorize].该应用程序正在使用表单身份验
生产服务器是Server 2008 SP 2(.NET 4.5.1和IIS 7).
堆栈跟踪的开始是:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +38
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +293
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +155
Run Code Online (Sandbox Code Playgroud)
我可以通过设置来修复它
<modules runAllManagedModulesForAllRequests="true">
Run Code Online (Sandbox Code Playgroud)
但是,我不喜欢使用这种大锤方法.
有没有更清洁的方法来解决这个问题?
sc字节代表什么?关于sc-bytes的每一次讨论和博客都表示,它代表从服务器到客户端的字节。
想知道例如sc字节是否代表总响应字节。当我们访问任何URL时,它是否包含css,js等的字节?
试过这样的事情:
HttpApplication app = s as HttpApplication; //s is sender of the OnBeginRequest event
System.Web.UI.Page p = (System.Web.UI.Page)app.Context.Handler;
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
lbl.Text = "TEST TEST TEST";
p.Controls.Add(lbl);
Run Code Online (Sandbox Code Playgroud)
运行时,我得到"对象引用未设置为对象的实例".为最后一行......
如何在原始文件中的特定loactions处插入两行文本(asp.net/html)?我怎么弄清楚文件的扩展名(我只想在aspx文件上应用这个...?
谷歌/ MyBrain让我失望.如果不使用框架(我永远不会超过我的同事),如果您(程序员)无法控制创建实例,那么如何将依赖注入HTTPModule?
我们是否正在攻击自定义的web.config部分+反射或者是否有更清洁的东西我没有看到?
例如,使用Karl Seguin的示例模块作为基础,并假设执行ILogger..Net 2.0 fwiw
public class ErrorModule : IHttpModule
{
private ILogger injectedLogger; //how to set this?
public void Init(HttpApplication application)
{
application.Error += (new EventHandler(this.application_Error));
}
public void Dispose() { /* required by IHttpModule */ }
private void application_Error(object sender, EventArgs e)
{
this.injectedLogger.doStuff(e.ExceptionObject as Exception);
}
}
Run Code Online (Sandbox Code Playgroud)
这样的事情让我意识到我有多鄙视MSDN.
我正在ASP.NET 3.5和IIS 7中开发一个应用程序.我编写了一个HTTP模块来执行URL重写,例如,我想将用户名重写为帐户ID"〜/ Profiles/profile.aspx?AccountID =" + account.AccountID.ToString();
见下文:
使用系统; 使用System.Collections.Generic; 使用System.Data; 使用System.Configuration; 使用System.IO; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.HtmlControls; 使用System.Web.UI.WebControls; 使用System.Web.UI.WebControls.WebParts; 使用System.Xml.Linq;
public class UrlRewrite : IHttpModule
{
private AccountRepository _accountRepository;
private WebContext _webContext;
public UrlRewrite()
{
_accountRepository = new AccountRepository();
_webContext = new WebContext();
}
public void Init(HttpApplication application)
{
// Register event handler.
application.PostResolveRequestCache +=
(new EventHandler(this.Application_OnAfterProcess));
}
public void Dispose()
{
}
private void Application_OnAfterProcess(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string[] …Run Code Online (Sandbox Code Playgroud) 我创建了一个HTTPModule,它应该记录reauest和响应.
告诉http模块如何在web.config中工作的参数.
但是,为了让http模块使用参数的最新值,我从app.config读取每个请求的参数.
有没有办法可以检测web.config中的更改,这样我可以重新加载http模块参数,而不是每次有响应时读取它们?
我有一个习惯IHttpModule,我只想在特定路线上工作.
例如 : http://example.com/HandleAzureTask
我希望只在/HandleAzureTask路由上调用/处理此模块.
由于这不是控制器,我无法真正设置[Authorize]它的属性; 如果用户通过身份验证,我该如何强制它才能被调用/处理?
我在ASP.NET MVC 4上,目前已将我的模块添加到web.config中:
<modules>
<remove name="AzureWebDAVModule" />
<add name="AzureWebDAVModule" type="VMC.WebDAV.Azure.Module.AzureWebDAVModule, VMC.WebDAV.Azure.Module" />
</modules>
Run Code Online (Sandbox Code Playgroud) 我有一个简单的Http模块:
public class CustomLoggingModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
context.EndRequest += EndRequest;
}
public void BeginRequest(object sender, EventArgs eventArgs)
{
//some code
}
public void EndRequest(object sender, EventArgs eventArgs)
{
//some
}
public void Dispose()
{
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能对此进行单元测试?特别是如何模拟事件?谁能举一些简单的例子?
httpmodule ×10
asp.net ×4
iis-7 ×3
c# ×2
.net ×1
.net-1.1 ×1
asp.net-mvc ×1
http ×1
iis ×1
iis-6 ×1
iis-7.5 ×1
mocking ×1
routes ×1
unit-testing ×1
web-config ×1