2016-11-10 274 views
1

我在頁面上收到404錯誤。我錯過了什麼?ASP.NET 4 MVC路由404

的Global.asax.cs:

protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
     } 

在App_Start文件夾RouteConfig.cs:

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

      routes.MapRoute(
       name: "ProductDetails", 
       url: "products/details", 
       defaults: new { controller = "ProductDetails", action = "Index" } 
      ); 

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

在我ProductDetailController.cs我:

public class ProductDetailsController : Controller 
    { 
     public ActionResult Index() 
     { 
      ProductModel Product = new ProductModel(); 
      //some code here 
      return View("~/Views/ProductDetails/Index.cshtml", Product); 
     } 
    } 

我的看法是位於該文件夾結構稱爲Index.cshtml。

當我查看網頁url/products/details /我得到一個404錯誤。我在這裏錯過了什麼?注意:這是一個Umbraco網站。

+0

嘗試使用默認約定來查看。 '返回視圖(產品);' – Nkosi

+1

您確定根據MVC約定將文件放入正確的文件夾中嗎?我建立了你的代碼,我沒有問題。我建議你重新訪問你的文件位置。 – Ismael

+0

我有Controllers文件夾中的ProductDetailsController.cs,我的View Index.cshtml位於Views文件夾下名爲ProductDetails的子文件夾中。 RouteConfig.cs位於App_Start文件夾中。 – user2928262

回答

1

一把umbraco接管所有路由,使以ovveride一把umbraco創建自定義路線創建一個名爲RoutingHandler類使用以下代碼在App_Start文件夾中:

using System.Web.Mvc; 
using System.Web.Routing; 
using Umbraco.Core; 

    public class RoutingHandler : ApplicationEventHandler 
    { 
     protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 
     { 
      RegisterCustomRoutes(); 
     } 

     private static void RegisterCustomRoutes() 
     { 
      RouteTable.Routes.MapRoute(
       name: "ProductDetails", 
       url: "products/details", 
       defaults: new { controller = "ProductDetails", action = "Index" } 
      ); 
     } 
    } 
0

嘗試只是作爲一個例子:編輯:在這個投多是我的看法Multi.cshtml名

public ActionResult Index(string playerId) 
    { 
     var model = new MultiPlayerLabelModel(); 

     try 
     { 
      var player = ServiceFactory.GetPlayerService().GetPlayer(playerId); 
      model = ServiceFactory.GetLabelService().GetLabelModels(player.Position, null); 
     } 
     catch (Exception) 
     { 
      LogService.Log.Error("Failed to generate label for player"); 
     } 

     return View("Multi", model); 
    }