0

我想知道如何UrlRedirectASP.NET MVC-4中保存 a :

例如我有这个:

/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
        );
    }
4

1 回答 1

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()});
于 2013-04-08T09:18:55.217 回答