简体   繁体   中英

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") });
}
 return Json(new { url = Url.Action("Index", "User", new { variableName= "abc" }) });

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:

在此处输入图片说明

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:

在此处输入图片说明

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:

在此处输入图片说明

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:

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM