奇怪的重定向行为

时间:2013-04-08 09:07:30

标签: redirect asp.net-mvc-4 url-routing

我想知道如何在 ASP.NET MVC-4 中保留 UrlRedirect

例如,我有这个:

/admin/category/add/81/142 

但在我使用之后:

return RedirectToAction("Add", "Category", new RouteValueDictionary
{
   {"id", siteid.Value},
   {"cid", pid.HasValue ? pid.Value : cid.Value }
});

我明白了:

/admin/category/add?id=81&cid=142

但我希望原来的 /admin/category/add/81/142

这是我的路由配置:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRouteLowercase(
            "Admin_LogOn",
            "Admin/{controller}/{action}/",
            defaults: new { controller = "Account", action = "LogOn" },
            constraints: null
        );

        context.MapRouteLowercase(
            "Admin_Category_List_Add",
            "Admin/{controller}/{action}/{id}/{cid}/{pid}",
            defaults:
                new
                {
                    controller = "Category",
                    action = "Create",
                    id = UrlParameter.Optional,
                    cid = UrlParameter.Optional,
                    pid = UrlParameter.Optional
                },
            constraints: null
        );


        context.MapRouteLowercase(
          "Admin_TwoParameters",
          "Admin/{controller}/{action}/{id}/{cid}",
          defaults: new { controller = "Manager", action = "Index", id = UrlParameter.Optional, cid = UrlParameter.Optional },
          constraints: null
      );


        context.MapRouteLowercase(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            defaults: new { controller = "Manager", action = "Index", id = UrlParameter.Optional },
            constraints: null
        );
    }

1 个答案:

答案 0 :(得分:1)

But i want the original one /admin/category/add/81/142

要保留该格式,您可以执行以下操作:

return Redirect(Url.Action("Add", "Category") + "/" + siteid.Value + "/" + (pid.HasValue ? pid.Value : cid.Value));

或者如果你喜欢连接字符串(我更喜欢第一个字符串):

return Redirect(new[] { Url.Action("Add", "Category"), "/", siteid.Value, "/", (pid.HasValue ? pid.Value : cid.Value).ToString()});