Dip Girase
Dip Girase

Reputation: 459

How to redirect URL with query string parameter in MVC4

I am trying to Request query string with parameter in show in redirect to URL like following URL but how to return write then do that RegNo is my string variable like abc please suggest me...

Url := /User/Index?abc

[HttpPost]
public ActionResult Putabc()
{
   return Json(new { url = Url.Action("Index","User"+"RegNo") });
}

Upvotes: 0

Views: 2524

Answers (3)

er-sho
er-sho

Reputation: 9771

If you want to pass only one parameter to url:

 public ActionResult Putabc()
 {
     return Json(new { url = Url.Action("Index", "User", new { RegNo = "abc" }) }, JsonRequestBehavior.AllowGet);
 }

Result:

enter image description here

If you want to pass more than one parameter to url

public ActionResult Putabc()
{
    return Json(new { url = Url.Action("Index", "User", new { RegNo = "abc", RegName = "pqr" }) }, JsonRequestBehavior.AllowGet);
}

Result:

enter image description here

In Url.Action:

The first parameter is your actionName.

The second parameter is your controllerName.

The third parameter is your routeValues means you can append one or more route values with this parameter.

Edit:

If you want to send parameter in route (/User/Index/RegNo) instead of query string (/User/Index?RegNo="abc")

If you want to send only one parameter in route

Then you need to define one route in RouteConfig.cs like

routes.MapRoute(
           "myRouteName",
           "User/Index/{RegNo}",
           new { controller = "User", action = "Index", RegNo = UrlParameter.Optional }
        );

And your action method will be

public ActionResult Putabc()
{
    return Json(new { url = Url.RouteUrl("myRouteName", new { RegNo = "abc" }) }, JsonRequestBehavior.AllowGet);
}

Result:

enter image description here

If you want to send more than one parameter in route

Then you need to define one route in RouteConfig.cs like

routes.MapRoute(
            "myRouteName",
            "User/Index/{RegNo}/AnyNameHere/{RegName}",
            new { controller = "User", action = "Index", RegNo = UrlParameter.Optional, RegName = UrlParameter.Optional }
        );

And your action method will be

public ActionResult Putabc()
{
    return Json(new { url = Url.RouteUrl("myRouteName", new { RegNo = "abc", RegName = "pqr" }) }, JsonRequestBehavior.AllowGet);
}

Result:

enter image description here

In above both routes User/Index/{RegNo} or User/Index/{RegNo}/AnyNameHere/{RegName} you can modify as your need.

In Url.RouteUrl:

The first parameter is your routeName that is myRouteName in your RouteConfig.cs.

The second parameter is your routeValues means you can append one or more route values with this parameter.

Upvotes: 1

Vishal Khatal
Vishal Khatal

Reputation: 119

 return Json(new { url = Url.Action("Index", "User", new { variableName= "abc" }) });

Upvotes: 1

Shani
Shani

Reputation: 11

@Html.Raw(Url.Content("~/AspNetWebForms/ViewPDF.aspx?id=" + docID.ToString() + "&small=True"))

Upvotes: 0

Related Questions