How to get key value from query string from SharePoint? - c#

I have an url: http://test?ID=i:0#.w|pro\administrator
I would like to get the current user (from Sharepoint) with c# code.
So I used the QueryString, here is:
private string userLogin = HttpContext.Current.Request.QueryString["ID"];
But the problem is that the value returned by QueryString is i:0.
Why?

Because that's what your URL has specified the value of the ID parameter is. # is a special character in URLs and everything after it is a separate part of the URL. You should URL encode the value of the parameter when constructing the URL in the first place to ensure that # (and any other characters) are treated as characters in value of the ID query parameter.

If you need to get current user, why do not use SP native method:
SPWeb web = SPControl.GetContextWeb(SPContext.Current);
uName = web.CurrentUser.ID;

Related

String Interpolation from URL in the database

I have a table in my database that keeps the route URL that I need to call.
For example I have this URL:
http://localhost/Account/Get/{id}
When I retrieve this value from the database, is coming into a variable. How can I do string interpolation to replace the {id} with another variable that I have with the value?

Handling special characters in query string

I have an ASP.NET Core app, that listens for a request from a third-party API, basically just a controller with the endpoint. This third-party API sends data in a query string, but one of the parameter names starts from #.
[HttpGet("auth")]
public IActionResult Auth([FromQuery] string access_token, [FromQuery] int expires_in)
{
return Ok();
}
This parameter is an access token value, so within URL it looks like this:
https://baseUrl?#access_token=something
So I need to declare it like this:
...([FromQuery] string #access_token,...
But we cannot use # in variable names, so how I should map this query parameter to my method parameter? Thanks.
the # is not included, it's just an indicator for repleaceable content. Faced a similar mistake with mongodb connect api, when they used to have # in their connection strings.
you can't have an # in the name of a variable/query string, it's just an indicator.
in third party api now, they use <> to mean replaceable content/values
like <access_token=something> or <access_token>.

how to send a url with query string (containing a url as a query string) as a query string

I need to redirect to a url which is sent to my application as a query string that containing a url as a query string. This can be repeated in three or four levels.
Suppose my application is accessible using example.com, and I have a request like:
http://example.com/?landing={http://example2.com/?landing={http://example3.com/?landing={http://google.com}&param=4}&param=3}&param=1
'{' and '}' are used to increase readability and do not exist in the urls actually.
I have to redirect to
http://example2.com/?landing={http://example3.com/?landing={http://google.com}&param=4}&param=3
example2 has to redirect to
http://example3.com/?landing={http://google.com}&param=4
and example3 has to redirect to http://google.com
I don't know how to sent the 'landing' query strings such that each query string be passed to the corresponding address to be consumed, or how to access the query strings correctly in my application, or on example2.com or example3.com.
Need your help, Tnx...
I solved the problem by encoding(urlencoding or base64encoding) the query strings in source and send the user to corresponding page. On that page the query string is decoded and the user is redirect to the next one query string and this process is done till the last landing page.

how does Request.QueryString work?

I have a code example like this :
location.href = location.href + "/Edit?pID=" + hTable.getObj().ID; ; //aspx
parID = Request.QueryString["pID"]; //c#
it works, my question is - how ? what is the logic ?
thanks :)
The HttpRequest class represents the request made to the server and has various properties associated with it, such as QueryString.
The ASP.NET run-time parses a request to the server and populates this information for you.
Read HttpRequest Properties for a list of all the potential properties that get populated on you behalf by ASP.NET.
Note: not all properties will be populated, for instance if your request has no query string, then the QueryString will be null/empty. So you should check to see if what you expect to be in the query string is actually there before using it like this:
if (!String.IsNullOrEmpty(Request.QueryString["pID"]))
{
// Query string value is there so now use it
int thePID = Convert.ToInt32(Request.QueryString["pID"]);
}
A query string is an array of parameters sent to a web page.
This url: http://page.asp?x=1&y=hello
Request.QueryString[0] is the same as
Request.QueryString["x"] and holds a string value "1"
Request.QueryString[1] is the same as
Request.QueryString["y"] and holds a string value "hello"
The Request object is the entire request sent out to some server. This object comes with a QueryString dictionary that is everything after '?' in the URL.
Not sure exactly what you were looking for in an answer, but check out http://en.wikipedia.org/wiki/Query_string
Request.QueryString["pID"];
Here Request is a object that retrieves the values that the client browser passed to the server during an HTTP request and QueryString is a collection is used to retrieve the variable values in the HTTP query string.
READ MORE#
http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx
The QueryString collection is used to retrieve the variable values in the HTTP query string.
The HTTP query string is specified by the values following the question mark (?), like this:
Link with a query string
The line above generates a variable named txt with the value "this is a query string test".
Query strings are also generated by form submission, or by a user typing a query into the address bar of the browser.
And see this sample : http://www.codeproject.com/Articles/5876/Passing-variables-between-pages-using-QueryString
refer this : http://www.dotnetperls.com/querystring
you can collect More details in google .

Requesting URL parameters in C#

The way I use to request parameters from URL is
if URL is- http://www.domain.ext/default.aspx?id=123&name=abc
In the above example we can request two parameter i.e. id and name from page default.aspx as string myid = Request["id"]; and string myname = Request["name"];
But in Facebook profile's URL it shows something of this kind.
http://www.facebook.com/john.deo or http://www.facebook.com/madcoder
Where there wont be any page name and to define name with syntax. How to request the parameters (john.deo or madcoder) from URL in C#?
This can be achieved by adding rules to an htaccess file.
RewriteRule ^([A-Za-z0-9_-]*)/$ index.php?name=$1
This rule will mean that if you type into the browser http://www.mydomain.co.uk/john you can get the value 'john' by requesting the 'name' parameter.
Those are not URL parameters, those are used to identify a resource.
You could use regex you're only objective is to get the "john.deo" in the given url
It all depends on how complex your solution is. But one easy way to do this is by using page routing.
You could register a route in your Global.asax -> Application_Start like this:
RouteTable.Routes.MapPageRoute("Route name", "requestformat", "physicalfile", checkPhysicalUrlAccess, routeValueDefaults, routeConstraints);
Then in your request format specify parameters like "{name}" and then default the name parameter in the ´routeValueDefaults´ dictionary and put a contstraint on it in the ´routeContstrains´ dictionary. The constraint can be specified by a reqular expression, so if you want the names to be all only characters you could use something like this "\w+".
The parameter then end up in the Page.RouteData["name"] collection of the physical page.

Categories

Resources