Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to route a query string with "?" and how to handle it

In my global asax file, I want to map a route such as this:

http://domain.com/add/link?url=http%3A%2F%2Fgoogle.com

And then catch it using my LinkController with action called Add.

Do I do this?

global.asax->

routes.MapRoute(
    "AddLink",
    "Add/Link?{url}",
    new { controller = "Link", action = "Add" }
);

LinkController->

public string Add(string url)
{
    return url; // just want to output it to the webpage for testing
}

?? That doesn't seem to work. What am I doing wrong? Thanks!

like image 370
Ian Davis Avatar asked Aug 29 '10 20:08

Ian Davis


People also ask

How do I get query string in react router?

Get a single Query String value location.search object: import React from 'react'; import { useSearchParams } from 'react-router-dom'; const Users = () => { const [searchParams] = useSearchParams(); console. log(searchParams); // ▶ URLSearchParams {} return <div>Users</div>; };

What is Route query?

The Route Query tool allows you to check where packets with a certain IP address are routed according to the current definitions. You can check that the routing is correct and quickly find a particular branch in the Routing tree.

How do I find Route parameters and query strings?

import ActivatedRoute from '@angular/router'. Inject ActivatedRoute class in constructor. Access queryParams property of ActivatedRoute class which returns an observable of the query parameters that are available in the current URL route.

How do you pass a query parameter in react router?

To pass in query parameters, we just add them to the Link s to props as usual. For example, we can write the following: We first defined the useQuery Hook to get the query parameters of the URL via the URLSearchParams constructor. We get the useLocation() s search property.


1 Answers

ASP.Net MVC will automatically bind parameters from the query string; you don't need to put it in the route.

Your route can simply be

routes.MapRoute(
    "AddLink",
    "Add/Link",
    new { controller = "Link", action = "Add" }
);
like image 118
SLaks Avatar answered Sep 19 '22 07:09

SLaks