【问题标题】:MVC RedirectToAction not hiding parametersMVC RedirectToAction 不隐藏参数
【发布时间】:2016-08-17 12:26:58
【问题描述】:

我有以下操作

public ActionResult TemplateBuilder(int id, int? processId) { }

然后我有以下

@Url.Action("TemplateBuilder","InspectionTemplate")/id/processId

然后网址看起来像:InspectionTemplate/TemplateBuilder/1/2

但是如果我使用

return RedirectToAction("TemplateBuilder","InspectionTemplate", new { id=1, processId = 2});

然后我得到以下结果:InspectionTemplate/TemplateBuilder/1?processId=2

我该如何解决这个问题。

这是我的路线

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.MapRoute(
            name: "IDRoute",
            url: "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Login",
                action = "Index",
                id = UrlParameter.Optional
            }
        );

        routes.MapRoute(
            name: "ProcessRoute",
            url: "{controller}/{action}/{id}/{processId}",
            defaults: new
            {
                controller = "InspectionTemplate",
                action = "TemplateBuilder",
                id = UrlParameter.Optional,
                processId = UrlParameter.Optional
            }
        );

        routes.MapRoute(
            name: "DateRoute",
            url: "{controller}/{action}/{year}/{month}/{day}",
            defaults: new
            {
                controller = "Inspection",
                action = "Assign",
                year = UrlParameter.Optional,
                month = UrlParameter.Optional,
                day = UrlParameter.Optional
            }
        );


    }

【问题讨论】:

  • id = UrlParameter.Optional,ProcessRoute中去掉(只有最后一个参数可以是可选的)并把它改成url: "InspectionTemplate/{action}/{id}/{processId}",所以它是特定的,并将它移动到第一条路线(顺序很重要)
  • 它不起作用:(
  • 没关系,它忘记了将它移到顶部。添加为答案,以便我接受它
  • 请注意,我回滚了您的编辑,以便答案和 cmets 有意义

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


【解决方案1】:

您的路线定义有 3 个问题

  1. 只有路由定义中的最后一个参数可以用 UrlParameter.Optional。如果您只提供一个,那么 路由引擎无法知道应该绑定哪个参数 因此值将作为查询字符串参数添加)。
  2. 然而,主要问题是路线的顺序。路由
    引擎按照声明的顺序搜索定义,并 一旦找到匹配的路径就会停止,在您的情况下,
    IDRoute 将匹配,因此需要交换路径。
  3. 然而,仅此一项可能会导致其他路由定义出现问题,因此 最好使您的路线具有唯一性,例如通过 在路由定义中指定控制器名称。

你的定义应该是

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "ProcessRoute",
        url: "InspectionTemplate/{action}/{id}/{processId}",
        defaults: new { controller = "InspectionTemplate", action = "TemplateBuilder", processId = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "DateRoute",
        url: "Inspection/{action}/{year}/{month}/{day}",
        defaults: new { controller = "Inspection", action = "Assign", } // assumes all parameters are required
    );

    // The default will only be match if the url does not start with '/InspectionTemplate/' or '/Inspection/'
    routes.MapRoute(
        name: "IDRoute",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
    );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多