mvc参数化路由给出404

本文关键字:路由 参数 mvc | 更新日期: 2023-09-27 18:08:08

我正在寻找一个URL: http://localhost/Details/1,但得到404。到目前为止,我已经完成了:

Global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace RMA
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Details",
                "Details/{action}/{id}",
                new { controller = "Details", action = "Index", id = UrlParameter.Optional }
            );
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

DetailsController.cs

public ActionResult Index(int id)
{
    return View();
}

mvc参数化路由给出404

首先,顺序很重要。在映射上从特定到一般。具体的映射应该放在第一位

但我不认为它会工作之后,要么,除非你有一个名为"1"的动作在细节控制器。;)

如果你想把它直接映射到控制器上而不做任何动作,试试像

这样的东西
routes.MapRoute(
                "Details",
                "Details/{id}",
                new { controller = "Details", action = "Index", id = UrlParameter.Optional }
            );