C# 带有一个必需参数和一个可选参数的 ASP.NET MVC 路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12739653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC routing with one mandatory parameter and one optional parameter?
提问by jebar8
I've been working on a large MVC application over the past month or so, but this is the first time I've ever needed to define a custom route handler, and I'm running into some problems. Basically I have two parameters to pass. The first one is required and the second one is optional.
在过去的一个月左右的时间里,我一直在开发一个大型 MVC 应用程序,但这是我第一次需要定义自定义路由处理程序,而且我遇到了一些问题。基本上我有两个参数要传递。第一个是必需的,第二个是可选的。
I'm following this answer here.
我在这里关注这个答案。
Here is my custom route:
这是我的自定义路线:
routes.MapRoute(
"MyRoute",
"{controller}/{action}/{param1}/{param2}",
new {
controller = "MyController",
action = "MyAction",
param1 = "",
param2 = "" // I have also tried "UrlParameter.Optional" here.
}
);
And my action method signature:
还有我的操作方法签名:
public ActionResult MyAction(string param1, string param2)
If I try the URL http://[myserver]/MyController/MyAction/Test1/Test2then it works like I expect it to, with param1 = "Test1" and param2 = "Test2"
如果我尝试 URL,http://[myserver]/MyController/MyAction/Test1/Test2那么它会像我期望的那样工作,param1 = "Test1" 和 param2 = "Test2"
If I try the URL http://[myserver]/MyController/MyAction/Test1then both parameters are null.
如果我尝试 URL,http://[myserver]/MyController/MyAction/Test1则两个参数都为空。
Hopefully somebody can tell me what I'm doing wrong here, because I'm lost.
希望有人能告诉我我在这里做错了什么,因为我迷路了。
采纳答案by Rafal
I assume that you created new route and left the default one that is very similar to yours. You should be aware that collection of routes is traversed to find first matching route. So if you have left the default one:
我假设您创建了新路线并保留了与您非常相似的默认路线。您应该知道遍历路由集合以找到第一个匹配的路由。因此,如果您保留了默认值:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
above your route then it will match request to http://[myserver]/My/MyAction/Test1and call MyController.MyActionand set "Text1" to parameter named id. Which will fail because this action is not declaring one named id.
在您的路线上方,然后它将匹配请求http://[myserver]/My/MyAction/Test1并调用MyController.MyAction并将“Text1”设置为名为 的参数id。这将失败,因为此操作未声明名为id.
What you need to do is to move your route as first in routes list and make it more specific then it is now:
您需要做的是将您的路线作为路线列表中的第一个移动并使其更具体,然后是现在:
routes.MapRoute(
"Route",
"My/{action}/{param1}/{param2}",
new
{
controller = "My",
action = "MyAction",
param1 = "",
param2 = ""
});
This will force all traffic routed trough Myto match this route.
这将强制所有路由槽的流量My匹配此路由。
回答by Rajpurohit
hi you create your rout like this i think this will hep you
嗨,你像这样创造了你的溃败,我想这会帮助你
routes.MapRoute(
"Regis", // Route nameRegister
"Artical/{id}", // URL with parameters
new { controller = "Artical", action = "Show", id = UrlParameter.Optional } // Parameter defaults
);
回答by Shivkumar
Try this
尝试这个
routes.MapRoute("MyRoute",
"myRoute/{param1 }/{param2 }",
new { controller = "MyController", action = "MyAction", param2 = UrlParameter.Optional },
new { param2 = @"\w+" });
you can specify one parameter as optional by using "UrlParameter.Optional" and specified second one with DataType means if you pass integer value then DataType (@"\d+") and for string i have mention above.
您可以使用“ UrlParameter.Optional”将一个参数指定为可选参数,并使用 DataType 指定第二个参数意味着如果您传递整数值,则 DataType (@"\d+") 和我在上面提到的字符串。
NOTE: Sequence of parameteris very important Optional parameter must pass at lastand register your new route Before Default Route In Gloab.asax.
注意: 参数的顺序非常重要可选参数必须最后通过并在 Gloab.asax 中的默认路由之前注册您的新路由。
then you action link like
然后你的动作链接就像
<a href="@Url.RouteUrl("MyRoute", new { param2 = "Test1",param1 = "Test2"})">Test</a>
OR with one parameter
或一个参数
<a href="@Url.RouteUrl("MyRoute", new { param2 = "Test1"})">Test</a>
In you Controller
在你的控制器
public ActionResult MyAction(string param2,string param1)
{
return View()
}

