ASP.NET 4 MVC路由404
问题描述:
我在页面上收到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网站。
答
一把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" }
);
}
}
答
尝试只是作为一个例子:编辑:在这个投多是我的看法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);
}
尝试使用默认约定来查看。 '返回视图(产品);' – Nkosi
您确定根据MVC约定将文件放入正确的文件夹中吗?我建立了你的代码,我没有问题。我建议你重新访问你的文件位置。 – Ismael
我有Controllers文件夹中的ProductDetailsController.cs,我的View Index.cshtml位于Views文件夹下名为ProductDetails的子文件夹中。 RouteConfig.cs位于App_Start文件夹中。 – user2928262