I have a search input type in my form. When the form is submitted, the page reloads with query params with +
So if I search for abc def the query param becomes ?q=abc+def the url becomes https://localhost:44300/Search?q=abc+def
Now this is sent to the webapi as an api call. so then on server side I am replacing the + with space then performing the search on the controller. Now when I search for abc def+efg it becomes abc+def%2Bdef
request.Query = request.Query.Replace('+', ' ');
request.Query = HttpUtility.UrlDecode(request.Query);
So on server side I am first replacing the param's +s with a space character then decoding it and finally I get abc def+efg on the controller which is what I was looking for.
All this could be avoided if on the form submission the url was encoded. so if from beginning the query param was ?q=abc%20def%2Befg I would just need to decode it.
How to do that?
Edit
The page renders first, then a vue component gets the query parameter, and makes a call to api controller with axios. /api/search
const searchQuery = this.$router.getQueryParam('q');
this.queryParams.query = searchQuery ? searchQuery : null;
return axios.get(`/api/search`, {
params: queryParams,
});
If you were submitting a form, the urleconding would be automatically applied. Since you are getting and sending the search query without form submission, just apply a javascript function to encode that fetched value:
this.queryParams.query = searchQuery ? encodeURI(searchQuery): null;
You can do like this.
Uri.EscapeUriString(request.Query)
Related
i want search spacial value in html code by webbrowser in c#. for example html code<span class="pulser " data-dollari="164.843956376000000" eq_toman="_XcUOV" pulser-change="_OiuVD" pre-dollari="164.964899983000000">$164.97</span>i need Getting the value "164.964899983000000" and another value html code.
If I understand you correctly, you want to get an element from a site and get its attribute values like 'pre-dollari'.
For c#, you can use ScrapySharp , it's a library where you can simulate a webbrowser and scrape its contents. You can use it alongside htmlAgilityPack
to effectively traverse the elements.
So for your case, it could look like this.
// get your Url
Uri url = new Uri("Yoursite.com");
// open up the browser
ScrapingBrowser browser = new ScrapingBrowser();
// navigate to your page
WebPage page = browser.NavigateToPage(url, HttpVerb.Post, "", null);
// find your element, convert to a list and take the first result [0]
HtmlNode node2 = page.Find("span", By.Class("pulser")).ToList()[0];
// and now you can get the attribute by name and put it in a variable
string attributeValue = node2.GetAttributeValue("pre-dollari", "not found");
// attributeValue = 164.964899983000000
I have a redirect:
string linkedInLogin = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=ID&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=ZEKNI&scope=r_basicprofile";
Response.Redirect(linkedInLogin);
This results in a redirect in my browser to
https://www.example.com/auth/linkedin?code=CODE_I_NEED&state=STATE
How do I retrieve the value of the code paramater?
Request.Querystring["code"] gives me a null value. The problem is that the browser "thinks" the current URL was /home/MyController.
try:
HttpContext.Current.Request.Params["code"]
this way you can get a combined collection of QueryString, Form, Cookies, and ServerVariables items.
here you can read more: HttpRequest.Params Property
Do you have that link stored somewhere or are you asking for how to retrieve it as well?
Assuming you already have it:
var s = "https://www.example.com/auth/linkedin?
code=CODE_I_NEED&state=ROB962242SMUT"
You can either do:
string s = "https://www.example.com/auth/linkedin?code=CODE_I_NEED&state=ROB962242SMUT";
var code = s.Substring((s.IndexOf("code=") + 5), (s.IndexOf('&') - (s.IndexOf("code=") + 5)));
//CODE_I_NEED
Or
String sourcestring = "https://www.example.com/auth/linkedin?
code=CODE_I_NEED&state=ROB962242SMUT";
Regex re = new Regex(#"code=(.*)&");
var result =Match m = re.Match(sourcestring).Value;
//CODE_I_NEED
edit: based on the other two answers i may not have understood your question properly xd
Basically my enum value determine's the page your on (in the url)and I have the below "if" statement to cover the stituation , if a user enters a load of text in the url it defaults to "New"......put if I enter any number other then the Enum values it displays that number instead of the default "New" page.
I don't want to post a load of code so hopefully it's clear, it redirects correctly if text entered in URL, but not for non-enum value. Is there a way to check if the Url input matches a enum value ?
ActionResult
[HttpGet]
public ActionResult Index(string status, string message)
{
var referralStatus = ReferralStatus.New;
if (!Enum.TryParse(status, out referralStatus))
{
referralStatus = ReferralStatus.New;
}
var model = new ReferralsModel();
model.Status = referralStatus.ToString();
model.ReferralsCount.Status = referralStatus.ToString();
return View(model);
}
Your url when landing on the Index page reads..
project/Referrals ,
your url after selecting a tab on the page reads...
project/Referrals?Status=2 ,
your url when entering diferent numbers in the Url
project/Refferals?status =56473 ,
your view renders with these numbers in place of enum status value.
please note this is an asp.net mvc 4 application
Edit : As stated in the comments, you may use Enum.IsDefined for parsing int representation of Enum
if (!Enum.TryParse(status, out referralStatus))
should be
if (!Enum.TryParse(status, out referralStatus) || !Enum.IsDefined(typeof(ReferralStatus),referralStatus))
From your comments, maybe you can try something like :
ReferralStatus referralStatus;
try{
referralStatus = (ReferralStatus) int.Parse(status);
}
catch{
status = ((int)ReferralStatus.New).ToString();
referralStatus = ReferralStatus.New;
}
I'm displaying a list of filtered items in a page, and now I have to limit the displaying by paginating the results.
So if I have url parameters like these:
example.com/?category=pizza&period=today
where both category and period can also not being showed:
example.com/?period=today
example.com/
how can I add a "Next page" in the end that keeps any previous parameter and adds
&pagenum=5
or if there are no parameters:
?pagenum=5
Tnx in advance!
For serverside
string url = Request.Url.GetLeftPart(UriPartial.Path);
url += (Request.QueryString.ToString() == "" ) ? "?pagenum=1" : "?" + Request.QueryString.ToString() + "&pagenum=1";
You can pass in the page number depending on how you are handling this.
For ASP.Net use the following:
string temp = Request.QueryString["yourParamName"];
Fissh
How do I request querystring using javascript from URL
e.g : http://localhost:1247/portal/alias__MySite/lang__en/tabid__3381/default.aspx
I want to get tabid...
var tabid = '<%= Request.QueryString["tabid"] %> ';
Above code works only in aspx page
but i dont need it, any ideas? thanks
There is now a new api URLSearchParams. Use that in conjunction with window.location.search
var urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.get('tabid'));
If your browser does not support URLSearchParams, you can create a custom fallback function:
function getParam(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
console.log(getParam('tabid'));
Don't know why but I've always found the javascript for querystring data fetching a bit hacky. if you don't need this value on the initial page load then perhaps you could use Request.QueryString in the code and set the value to a hidden field, which your javascript will read from?
Try this, It is working perfectly for me.
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var tabId=getParameterByName("tabid");
I bet there is a server-side rewrite (DotNetNuke?), so the aspx.cs "sees" the redirection target which contains the correct QueryString.
For the client, you have to use another mechanism because the browser only "sees" the public URL. In this case, a Regex that picks the number behind 'tabid_' and before the next slash should work. This would be the same number (page id?) that the aspx page "sees".
This is what I used:
<script type="text/javascript">
function QueryString(key) {
//Get the full querystring
fullQs = window.location.search.substring(1);
//Break it down into an array of name-value pairs
qsParamsArray = fullQs.split("&");
//Loop through each name-value pair and
//return value in there is a match for the given key
for (i=0;i<qsParamsArray.length;i++) {
strKey = qsParamsArray[i].split("=");
if (strKey[0] == key) {
return strKey[1];
}
}
}
//Test the output (Add ?fname=Cheese&lname=Pizza to your URL)
//You can change the variable to whatever it is you need to do for example, you could
//change firstname to id and lastname to userid and just change the reference in the
//document.write/alert box
var firstname = QueryString("fname");
var lastname = QueryString("lname");
document.write("You are now logged in as " + firstname + " " + lastname + "!");
</script>
You can replace document.write with alert and it would give you an alert box instead!
I used this on my website. Its not done yet but when it is it will be at zducttapestuff.com
The output will look like this: You are now logged in as Cheese Pizza!
This is very unsecure for Passwords though since the password will be shown in the url.