ASP.NET 4 MVC路由404

时间:2016-11-10 22:08:35

标签: c# asp.net asp.net-mvc-4 asp.net-mvc-routing umbraco

我的网页上出现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网站。

2 个答案:

答案 0 :(得分:1)

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" }
            );
        }
    }

答案 1 :(得分:0)

试着举个例子:编辑:在这个演员表中Multi是我的View 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);
    }