我有以下IHttpModule
,我試圖找出如何從給定的絕對或相對URL的控制器執行操作。如何從ASP.NET MVC中的HttpModule執行控制器動作?
public class CustomErrorHandlingModule : IHttpModule
{
#region Implementation of IHttpModule
public void Init(HttpApplication context)
{
context.Error += (sender, e) =>
OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
}
public void Dispose()
{}
public void OnError(HttpContextBase context)
{
// Determine error resource to display, based on HttpStatus code, etc.
// For brevity, i'll hardcode it for this SO question.
const string errorPage = @"/Error/NotFound";
// Now somehow execute the correct controller for that route.
// Return the html response.
}
}
這怎麼辦?
什麼Fx版本?這是僅用於正常操作還是錯誤? –
你真的讀過_all_路由問題嗎? –
@Henk Holterman - > .NET 4.0(我會更新這篇文章)。錯誤是唯一的。看看它是如何觸發'context.Error'?當然,我沒有閱讀*所有* ..關於MVC和404處理的一些流行的。 –