为RouteConfig表定义一个新的可选参数

时间:2013-11-28 10:35:16

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我一直在使用默认路由表,如下所示: -

public class RouteConfig
    {

        public static void RegisterRoutes(RouteCollection routes)
        { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
          routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
            ); } }

在共享视图上,我有以下Html.actionlink,作为我的面包屑的一部分来引用当前页面: -

@Html.ActionLink(aoutput == "Index" ? "Home" : aoutput , action, controller )

只要URL中的参数为id,它就能正常工作,但在某些操作方法中,它们接受名为customername的参数,如下所示: -

…./Customer/ManageCustom?customerName=name123

但是Html.ActionLink将无法从当前URL引用customerName,因为在我的路由映射中没有对customerName参数的引用。所以有人可以据此提出建议 此致

修改

ManageCustom Action方法如下所示: -

public ActionResult ManageCustom(string customerName)
       {

          //code goes here
           return View( customAssets);

       }

1 个答案:

答案 0 :(得分:1)

您指定的控制器操作的url不多。尝试将ManageCustom更改为ManageCustomAsset,您会看到它正常工作。

如果您更改操作名称或使用值ManageCustom的{​​{3}},它也应该有用。 ActionName属性的示例:

[ActionName("ManageCustom")]
public ActionResult ManageCustomAsset(string customerName) 
{
    //code goes here
    return View( customAssets);
}

您的默认路由期望您将可选ID添加到资源字符串,它将如下所示: /Customer/ManageCustom/123,其中123 - 是id。

但是你要通过查询字符串将参数设置为动作,这就是“?”之后的内容。签名和路由对此没有任何限制。