ASP.NET 4 个 MVC 路由 404
ASP.NET 4 MVC Routes 404
我的页面出现 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 网站。
举个例子:编辑:在这个 cast 中,Multi 是我的视图的名称 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);
}
Umbraco 接管所有路由,因此要覆盖 Umbraco 以创建自定义路由,请使用以下代码在 App_Start 文件夹中创建一个名为 Routing Handler 的 class:
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" }
);
}
}
我的页面出现 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 网站。
举个例子:编辑:在这个 cast 中,Multi 是我的视图的名称 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);
}
Umbraco 接管所有路由,因此要覆盖 Umbraco 以创建自定义路由,请使用以下代码在 App_Start 文件夹中创建一个名为 Routing Handler 的 class:
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" }
);
}
}