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?
Related
I'm populating a model in an mvc controller, the model has a public string of Name which is to capture the AD name of the users using the web app.
in the controller in debug model.Name has the right value. So something like DOMAIN\rjones.
the problem is if a user has an initial of r or n then the \r or \n (maybe other chars) are getting stripped out.
I'm passing the values to sql by the following:
db.Database.ExecuteSqlCommand("EXEC dev.uspCreateNewDevCase #LoggedBy, #LoggedFor, #ShortDescription, #LongDescription, #DateDue"
, new SqlParameter("#LoggedBy", model.Name )
, new SqlParameter("#LoggedFor", model.LoggedFor)
, new SqlParameter("#ShortDescription", model.ShortDescription)
, new SqlParameter("#LongDescription", model.LongDescription)
, new SqlParameter("#DateDue", model.DateDue)
in debug, the model.Name , new SqlParameter("#LoggedBy", model.Name ) has the correct string text.
I have stripped the stored procedure down in sql to just write that value to a table, and when I query the table it has the value of DOMAIN jones the \r has been taken out.
As mentioned this is dynamic as in it will depend on the user using the app if they have an r or n in the name. Is there a way to encode this or preserve it so when it passed to SQL it maintains the \r or \n.
I'm assuming it is being lost in C# as in T-SQL you can store that value in a variable and insert it into a table. The T-sql data type is NVARCHAR
fixed
I was passing the value to the controller by an Ajax method of:
$.ajax({
type: "POST",
url: urlAction,
data: JSON.stringify({
Name: #HttpContext.Current.User.Identity.Name ,
However, I have set the model value in the view via #Html.HiddenFor to be $.ajax({
type: "POST",
url: urlAction,
data: JSON.stringify({
Name: #HttpContext.Current.User.Identity.Name , which passes the value with a \ in it and then writes correctly to the SQL table.
If you set a variable this way:
string myString="This is\rmy text"; -> "This is" + (char)13 + "my text"
It will be stored with a replace of \r as (char)13 (that means the ASCII code CR for Carriage Return)
If you double the \ it won't
string myString="This is\\rmy text"; -> "This is\rmy text"
Another general way is to start the string with the "at"-sign:
string myString=#"This is\rmy text"; -> "This is\rmy text"
Are you sure, that the variable contains the right value before you pass it?
EDIT:
What you see might not be what is there. Look at this:
The preview on mouse hover shows the \r sign, but if you click the lense you get:
So: please check again, if you really have what you think that you have...
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;
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 .
i just want to get full querystring from url.
Request.QueryString
Request.ServerVariables["QUERY_STRING"]
Can i use any one of these?
which way is preferred?
Thanks
Request.ServerVariables["QUERY_STRING"] contains the entire query string, that is everything after the question mark but before the fragment identifier #
http://msdn.microsoft.com/en-us/library/ms525396(v=vs.90).aspx
Request.QueryString Contains a collection allowing you to get individual elements.
Using following syntax:
Request.QueryString(variable)[(index)|.Count]
This collection is generated from the ServerVariables collection. The values in this collection are automaticly UrlDecoded.
So if you call Request.QueryString.ToString(), it is inherently the same as Request.ServerVariables["QUERY_STRING"], but with UrlDecoding.
So you should use this as it is safer.
Request.QueryString(variable)[(index)|.Count]
http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx
http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx
The QueryString collection is a parsed version of the QUERY_STRING variable in the ServerVariables collection. It enables you to retrieve the QUERY_STRING variable by name. The value of Request.QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request.QueryString(parameter).Count. If a variable does not have multiple data sets associated with it, the count is 1. If the variable is not found, the count is 0.
To reference a QueryString variable in one of multiple data sets, you specify a value for index. The index parameter can be any value between 1 and Request.QueryString(variable).Count. If you reference one of multiple QueryString variables without specifying a value for index, the data is returned as a comma-delimited string.
When you use parameters with Request.QueryString, the server parses the parameters sent to the request and returns the specified data. If your application requires unparsed QueryString data, you can retrieve it by calling Request.QueryString without any parameters.
If you call Request.QueryString["Whatever"] than UrlDecode is automatically executed. See does Request.Querystring automatically url decode a string? . So be careful with your spaces, %20, ampersands etc.
Regards,
Michael
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.