ASP.NET MVC有一个强制参数和一个可选参数路由? [英] ASP.NET MVC routing with one mandatory parameter and one optional parameter?

查看:384
本文介绍了ASP.NET MVC有一个强制参数和一个可选参数路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在过去一个月左右的时间在一个大的MVC应用程序,但是这是我曾经需要自定义一个路由处理的第一次,我遇到了一些问题。基本上我有两个参数来传递。第一个是必需的,第二个是可选的。

我在这里下面这个答案

下面是我的自定义路线:

  routes.MapRoute(
    MyRoute
    {控制器} / {行动} / {参数1} / {参数2},
    新{
        控制器=myController的
        行动=MyAction,
        参数1 =,
        参数2 =//我也曾尝试UrlParameter.Optional在这里。
    }
);

和我的操作方法签名:

 公众的ActionResult MyAction(字符串参数1,字符串参数2)

如果我尝试网址的http:// [MYSERVER] / myController的/ MyAction / Test1的/ Test2的然后它就像我希望它与参数1 =测试1 和参数2 =Test2的<​​/ p>

如果我尝试网址的http:// [MYSERVER] / myController的/ MyAction / Test1的那么这两个参数为null

希望有人能告诉我什么,我做错了,因为我迷路了。


解决方案

我假设你创建了新的途径,离开了默认的一个是与你的差不多。你应该知道的路由集合运行到找到第一个匹配的路由。所以,如果你已经离开了默认的:

  routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
        );

您的路线上方,然后它会匹配请求的http:// [MYSERVER] /我的/ MyAction / Test1的并调用 MyController.MyAction ,并设置文本1到参数名为 ID 。这是因为这个动作是不是宣告一个命名的失败 ID

您需要做的是将您的路线首先在路由表,使之更加具体的话,现在是:

  routes.MapRoute(
            路线,
            我/ {行动} / {参数1} / {参数2},
            新
            {
                控制器=我,
                行动=MyAction,
                参数1 =,
                参数2 =
            });

这将迫使所有的流量路由的低谷我的来搭配这条路线。

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.

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/Test2 then it works like I expect it to, with param1 = "Test1" and param2 = "Test2"

If I try the URL http://[myserver]/MyController/MyAction/Test1 then both parameters are null.

Hopefully somebody can tell me what I'm doing wrong here, because I'm lost.

解决方案

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/Test1 and call MyController.MyAction and set "Text1" to parameter named id. Which will fail because this action is not declaring one named 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 My to match this route.

这篇关于ASP.NET MVC有一个强制参数和一个可选参数路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆