2011-11-02 63 views
0

我試圖在我的web應用程序中設置路由。路由和Ninject給出404

但是它似乎不適用於Ninject。如果我在Global.asax中評論所有的Ninject,這條路線就像一個魅力。

在Ninject文件中,當試圖打開路由頁面時,我只是得到一個404「無法找到資源」。

繼承人是什麼在我的Global.asax代碼:

<%@ Application Language="C#" Inherits="Ninject.Web.NinjectHttpApplication" %> 
<%@ Import Namespace="Infrastructure.Storage" %> 
<%@ Import Namespace="Ninject" %> 
<%@ Import Namespace="Ninject.Modules" %> 
<%@ Import Namespace="System.Web.Routing" %> 

<script runat="server"> 

void Application_Start(object sender, EventArgs e) 
{ 
    RegisterRoutes(RouteTable.Routes); 
} 

void Application_End(object sender, EventArgs e) 
{ 
    // Code that runs on application shutdown 

} 

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs 

} 

void Session_Start(object sender, EventArgs e) 
{ 
    // Code that runs when a new session is started 

} 

void Session_End(object sender, EventArgs e) 
{ 
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode 
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised. 

} 

protected override IKernel CreateKernel() 
{ 
    IKernel kernel = new StandardKernel(new SiteModule()); 
    return kernel; 
} 

public class SiteModule : NinjectModule 
{ 
    public override void Load() 
    { 
     //Bind<ILogger>().To<NLogger>().InSingletonScope(); 
     //Bind<IAuthentication>().To<Authentication>(); 
     Bind<ISession>().To<LinqToSqlSession>(); 
     Bind<IReadOnlySession>().To<LinqToSqlReadOnlySession>(); 
     //Bind<IReporting>().To<LinqToSqlReporting>(); 
    } 
} 

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapPageRoute("City", "Cities/{id}", "~/test2.aspx"); 
} 

</script> 

任何人都有可能是錯誤的想法?

回答

0

當您爲asax使用NinjectHttpApplication類時,需要更改applicationStart & applicationEnd的調用方式。

發生什麼事是.net自動將上述方法連接到相應的事件。因爲NinjectHttpApplication已經處理了Application_Start,所以你的方法不會被調用,因此你的路由沒有被註冊。您需要將該方法更改爲

protected override void OnApplicationStarted() 
{ 
    base.OnApplicationStarted(); 

    RegisterRoutes(RouteTable.Routes); 
}