2013-07-24 120 views
0

我創建了一個新的mvc項目。之後我添加了舊的Web表單內容。在我的舊web表單頁面或配置文件中,有些東西阻止了我的項目中的路由,並且/ Home/Index無法正常工作。在我的mvc項目中路由不起作用

但我真的無法找到任何錯誤的東西。請幫忙。

這是我的Global.asax一部分,我沒有做出以下缺省類

 AreaRegistration.RegisterAllAreas(); 

     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 

爲我難過,我沒有改變上述類別的任何事物的任何變化,這是我的RegisterRoutes方法內容

 public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.IgnoreRoute("{resource}.aspx/{*pathInfo}"); 

      routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
     } 

和我的web配置的部分

<compilation debug="true" targetFramework="4.5" defaultLanguage="c#" optimizeCompilations="true"> 
    <assemblies> 
    <add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
    <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
    <add assembly="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
    <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
    <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.Drawing.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
    <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
    <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
    <add assembly="System.Web.DataVisualization.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
    <add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
    <add assembly="office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/> 
    <add assembly="Microsoft.Vbe.Interop, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/> 

    </assemblies> 

    ... 

    <namespaces> 
    <add namespace="System.Web.Helpers" /> 
    <add namespace="System.Web.Mvc" /> 
    <add namespace="System.Web.Mvc.Ajax" /> 
    <add namespace="System.Web.Mvc.Html" /> 
    <add namespace="System.Web.Routing" /> 
    <add namespace="System.Web.WebPages" /> 
    </namespaces> 

    ... 

    <modules runAllManagedModulesForAllRequests="false"> 
     <remove name="Session"/> 
     <add name="Session" type="System.Web.SessionState.SessionStateModule"/> 
     <remove name="RequestControlModule"/> 
     <add name="RequestControlModule" preCondition="managedHandler" type="RequestControlModule" /> 
     <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /> 
    </modules> 

回答

0

嘗試改變RegisterRou te方法如下:

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.MapRoute(null, 
       "{controller}/{action}", 
       new 
       { 
        controller = "Home", 
        action = "Index"  
       } 
       ); 

       routes.MapRoute(
        "Default", // Route name 
        "",  // URL with parameters 
        new { controller = "Home", action = "Index" } // Parameter defaults 
       ); 
    } 
+0

我編輯了我的問題, – brtb

+0

旁邊我的方法是自動生成的VS 2012,你聲稱這是錯誤的?你的答案不起作用,但你能解釋一下爲什麼你認爲這可能是一個解決方案? – brtb

+0

請在您的HomeController類中提供您的Index方法的列表。上面的答案不是你需要的答案,我們會在我們弄明白的情況下用正確的答案替換它。它應該看起來像這樣:公共類HomeController:控制器{公衆的ViewResult指數(){ 返回視圖(); } } –