在MVC操作中传递参数

本文关键字:参数 MVC 操作 | 更新日期: 2023-09-27 18:25:19

我有一个典型的MVC应用程序,它具有以下操作:

public ActionResult MyAction(string param1)

当我这样称呼这个动作时:

http://domain/MyController/MyAction?param1=dasas

我收到param1作为假设:"dasas"

但在MVC中,这个想法是按照如下方式传递参数,对吗?

http://domain/MyController/MyAction/dasas

这样它不起作用,我想知道为什么!知道吗?

更新:这是我的路线:

routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: 
                new { 
                    controller = MVC.RegistrationAuthority.Name,
                    action = MVC.RegistrationAuthority.ActionNames.Landing, 
                    id = UrlParameter.Optional 
                });

在MVC操作中传递参数

您需要配置您的路由,以知道传入的参数称为param1

开箱即用的asp.net mvc仅适用于{id}参数

routes.MapRoute(null, "MyController/MyAction/{param1}", new { controller = "MyController", action = "MyAction"});

请确保将此路由添加到可能覆盖它的其他路由(即默认路由)

之前