hackp0int
hackp0int

Reputation: 4169

Strange redirect behavior

I would like to know how to preserve a UrlRedirect in ASP.NET MVC-4:

For example i have this:

/admin/category/add/81/142 

But after i use this:

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

I get this:

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

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

Here is my Routing configuration:

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

Upvotes: 0

Views: 92

Answers (1)

von v.
von v.

Reputation: 17108

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

To preserve that format you can do this:

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

or if you prefer to concat strings (I prefer the first one):

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

Upvotes: 1

Related Questions