2016-09-29 29 views
0

這是我在角JS 2 MVC4.5應用routing.ts角JS 2 MVC 4.5在瀏覽器的URL按Enter鍵顯示404個

   import { ModuleWithProviders } from '@angular/core'; 
       import { Routes, RouterModule } from '@angular/router'; 

       import { HomeComponent }  from './components/home/home.component'; 
       import { CarsComponent }  from './components/cars/cars.component'; 

       const appRoutes: Routes = [ 
        { 
         path: 'home', 
         component: HomeComponent 
        }, 
        { 
         path: 'cars', 
         component: CarsComponent 
        }, 
        { 
         path: '', 
         redirectTo: '/home', 
         pathMatch: 'full' 
        }, 
       ]; 

       export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

這工作得很好,但我按http://localhost:3000/cars進入

enter image description here

它會顯示一個404錯誤頁面

enter image description here

它在服務器上搜索localhost:3000/cars並且服務器沒有這個端點如何解決這個問題?

在服務器側的服務器側WebApiConfig

    public static class WebApiConfig 
        { 
         public static void Register(HttpConfiguration config) 
         { 
          // Web API configuration and services 

          // Web API routes 
          config.MapHttpAttributeRoutes(); 

          config.Routes.MapHttpRoute(
           name: "DefaultApi", 
           routeTemplate: "api/{controller}/{id}", 
           defaults: new { id = RouteParameter.Optional } 
          ); 
         } 
        } 
+0

每次請你從瀏覽器發送服務器進行處理。當您按下地址欄中的回車鍵時,它會將請求發送到服務器。但是你在服務器中沒有'/ cars'路徑。這就是爲什麼它顯示404錯誤。 –

+0

@ ti2005嗯尋找一個解決這個問題 – Kalanamith

回答

0

嘗試在Global.asax文件上Application_Start改變模塊登記序列像這樣的:

protected void Application_Start() 
{ 
    GlobalConfiguration.Configure(WebApiConfig.Register); // this line should be placed on top of all routes 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
} 

標準路由配置似乎與的WebAPI路由插手(可能由於RegisterRoutes在你的問題上首先聲明,它假定來自瀏覽器的地址欄的所有請求都是HTTP請求而不是WebApi),因此您的WebApi路由localhost:3000/cars通過RegisterRoutes模塊作爲CarsController的GET方法處理並返回404頁面。

歡迎任何建議。

類似的問題:Web Api 404 error The resource cannot be found

0

這解決了問題

 <configuration> 
      <system.web> 
      <compilation debug="true" targetFramework="4.5" /> 
      <httpRuntime targetFramework="4.5" /> 
      </system.web> 

      <system.webServer> 
      <rewrite> 
       <rules> 
       <rule name="Main Rule" stopProcessing="true"> 
        <match url=".*" /> 
        <conditions logicalGrouping="MatchAll"> 
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
        </conditions> 
        <action type="Rewrite" url="/" /> 
       </rule> 
       </rules> 
      </rewrite> 
      </system.webServer> 

     </configuration> 

添加了這個客戶端的web.config

相關問題