Home > Blockchain >  Retrieve Query String Parameter Value Containing URL in MVC Controller
Retrieve Query String Parameter Value Containing URL in MVC Controller

Time:12-24

I want to retrieve the query string value, but the query string parameter value contains the URL of another page, and not getting whole URL.

Check the Attached Screenshot.

In the image, I want to access the "id" paramter value that is "/defaulthealthchecktemplateitem/Item?id=1006&templateid=3&backTo=defaulthealthchecktemplateitem" but getting only "/defaulthealthchecktemplateitem/Item?id=1006".

  [TypeFilter(typeof(AuthorizationPrivilegeFilter))]
  [Authorize]
  [Route("home")]
  public class HomeController : Controller
  {
    public IActionResult Index(string id)
    {
      ViewBag.RedirectUrl = id ?? string.Empty;
      return View();
    }
  }

enter image description here

CodePudding user response:

It's not an issue with the server-side code, it's an issue with the URL. Because the URL contains query string arguments, there is currently in this example no way to discern between which arguments belong to the URL being requested and which belong to the URL that is itself a parameter value.

The way you account for this is by URL-encoding that value. So this:

/defaulthealthchecktemplateitem/Item?id=1006&templateid=3&backTo=defaulthealthchecktemplateitem

Should be this:

/defaulthealthchecktemplateitem/Item?id=1006&templateid=3&backTo=defaulthealthchecktemplateitem

The framework should automatically URL-decode the value when you retrieve it in the server-side code.

  • Related