Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 .NET MVC布线工程,但一件除外_Asp.net Mvc 3_Model View Controller_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc 3 .NET MVC布线工程,但一件除外

Asp.net mvc 3 .NET MVC布线工程,但一件除外,asp.net-mvc-3,model-view-controller,asp.net-mvc-routing,Asp.net Mvc 3,Model View Controller,Asp.net Mvc Routing,这里没有什么帮助和建议 我正在开发我的第一个MVC应用程序,我已经建立了一个学生实体。 具有基本CRUD功能的学员控制器和视图。 mysite.com/Student让我到达那里 现在我想添加付款,所以我添加了一个带有基本crud的付款控制器和视图。 这给了我mysite.com/Payments 我希望付款转到一个类似于:mysite.com/Student/payments的URL 所以我研究了URL路由,并且(我想)我有很长一段时间都是反向的,因为似乎什么都不起作用。但现在,我创建了这个额

这里没有什么帮助和建议

我正在开发我的第一个MVC应用程序,我已经建立了一个学生实体。
具有基本CRUD功能的学员控制器和视图。 mysite.com/Student让我到达那里

现在我想添加付款,所以我添加了一个带有基本crud的付款控制器和视图。 这给了我mysite.com/Payments

我希望付款转到一个类似于:mysite.com/Student/payments的URL

所以我研究了URL路由,并且(我想)我有很长一段时间都是反向的,因为似乎什么都不起作用。但现在,我创建了这个额外的路线:

        routes.MapRoute(
            "Payments",
            "Student/Payments/{action}/{id}",
            new { Controller = "Payments", action = "Index", id = UrlParameter.Optional }
            );
现在一切似乎都正常工作了。当我向Payment controller中的任何操作发送ActionLink时,URL是正确的。例如:www.mysite.com/Student/Payments/Edit/5作为URL出现

我遇到的问题是,支付仍然是一个基本的URL路由。因此,我也可以通过访问www.mysite.com/payments获得付款

如何“删除”该路由,使mysite.com/Payments无效?还是我在某种程度上把事情搞得一团糟


谢谢你的帮助

你对这件事的思考方式是扭曲的。映射配置只提供规则的层次列表,以指定特定url的代码所在的位置

所以当你说它仍然在访问mysite.com/Payments时。这是因为它符合Global.asax中的默认规则

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );
您可以删除此项,但默认规则将不起作用

或者您可以添加忽略规则。就你的情况来说

        routes.IgnoreRoute("Payments/{action}/{id}");

确保你把它放在默认规则之上。

你的思考方式是扭曲的。映射配置只提供规则的层次列表,以指定特定url的代码所在的位置

所以当你说它仍然在访问mysite.com/Payments时。这是因为它符合Global.asax中的默认规则

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );
您可以删除此项,但默认规则将不起作用

或者您可以添加忽略规则。就你的情况来说

        routes.IgnoreRoute("Payments/{action}/{id}");

确保将其置于默认规则之上。

您需要对默认路由使用MapRoute重载方法,即:

routes.MapRoute(  
  "Default",  
  "{controller}/{action}/{id}",  
  new { controller = "Home", action = "Index" },
  new { controller = ""}); //there constraints for controller goes

关于创建自定义约束,有一个“不等于”的示例,您需要对默认路由使用MapRoute方法重载,即:

routes.MapRoute(  
  "Default",  
  "{controller}/{action}/{id}",  
  new { controller = "Home", action = "Index" },
  new { controller = ""}); //there constraints for controller goes
看看关于创建自定义约束,有一个“不等于”的例子