MVC3参数化路由给出了404

时间:2014-06-11 20:56:48

标签: c# asp.net asp.net-mvc

我正在寻找一个网址: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();
}

1 个答案:

答案 0 :(得分:6)

首先,订单很重要。在映射上从具体到一般。详细信息 - 特定映射应该是第一个。

但我不认为它会在那之后工作,除非你有一个名为" 1"在Details控制器上。 ;)

如果您想在没有操作的情况下将其直接映射到控制器,请尝试类似

的操作
routes.MapRoute(
                "Details",
                "Details/{id}",
                new { controller = "Details", action = "Index", id = UrlParameter.Optional }
            );