C# 在MVC操作中传递参数
我有一个典型的MVC应用程序,它具有以下操作:C# 在MVC操作中传递参数,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我有一个典型的MVC应用程序,它具有以下操作: public ActionResult MyAction(string param1) 当我这样称呼这个动作时: http://domain/MyController/MyAction?param1=dasas 我收到了param1,应该是:“dasas” 但是在MVC中,想法是按如下方式传递参数,对吗 http://domain/MyController/MyAction/dasas 这种方式不起作用,我想知道为什么!有什么想法吗 更新:
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
});
您需要配置路由以知道传入的参数被称为
param1
开箱即用的asp.net mvc仅用于{id}
参数
routes.MapRoute(null, "MyController/MyAction/{param1}", new { controller = "MyController", action = "MyAction"});
确保在可能覆盖此路由的其他路由(即默认路由)之前添加此路由。是否不应使用默认路由(我更新了问题)?我试着添加你的代码,但它仍然是空的。我改变了规则的顺序,最后使用了更通用的一个,它工作了。安东尼,你的回答帮助了我。是的,对不起。我应该说得很具体,你最通用的路由需要先来“但在MVC中,想法是按如下方式传递参数”-没有必要-由你决定什么应该是url的一部分,什么应该作为查询参数传递。我想这已经得到了链接副本的回答。当遵循建议一式两份(与Anthony Shaw的答案相同)时,请确保正确订购路线-更具体的路线应在更通用的路线之前,例如默认路线(即“/{controller}”之前的“/foo”)。