What is the difference between HttpContext.Current.Request.Url.OriginalString and HttpContext.Current.Request.Url.AbsoluteUri?
I need to get the URL in the page load, but confused about the both.
Depending on the url, you could get the same result. It depends how exact you want it to be, if you want all fragments, and how you want to use it.
The following is for Uri but I think it applies to Url too (a url is a uri, essentially).
AbsoluteUri
The AbsoluteUri property includes the entire URI stored in the Uri instance, including all fragments and query strings.
This
Uri baseUri= new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri,"catalog/shownew.htm?date=today");
Console.WriteLine(myUri.AbsoluteUri);
will return
http://www.contoso.com/catalog/shownew.htm?date=today
https://msdn.microsoft.com/en-us/library/system.uri.absoluteuri(v=vs.110).aspx
Uri.OriginalString
Gets the original URI string that was passed to the Uri constructor.
The following example creates a new Uri instance from a string. It illustrates the difference between the value returned from OriginalString, which returns the string that was passed to the constructor, and from a call to ToString, which returns the canonical form of the string.
// Create a new Uri from a string address.
Uri uriAddress = new Uri("HTTP://www.ConToso.com:80//thick%20and%20thin.htm");
// Write the new Uri to the console and note the difference in the two values.
// ToString() gives the canonical version. OriginalString gives the orginal
// string that was passed to the constructor.
// The following outputs "http://www.contoso.com/thick and thin.htm".
Console.WriteLine(uriAddress.ToString());
// The following outputs "HTTP://www.ConToso.com:80//thick%20and%20thin.htm".
Console.WriteLine(uriAddress.OriginalString);
https://msdn.microsoft.com/en-us/library/system.uri.originalstring(v=vs.110).aspx
Related
I have following request mapping.
#RequestMapping(value = "/{userId}", produces = { MediaTypes.HAL_JSON_VALUE })
#ResponseStatus(HttpStatus.OK)
public Resource<UserDTO> findUser(#PathVariable final String userId) {
User user = administrationService.getSecurityUserById(userId);
return userResourceAssembler.toResource(modelMapper.map(user, UserDTO.class));
}
I use RestTemplate to get the resource. The user ids passed in the url contain a dot (.) like: john.doe -> request URL: http://mysite/users/john.doe
When the above method gets called I only get john in #PathVariable userId.
How can this be fixed? to get john.doe
Thx.
anything that goes after a dot in RequestMapping is considered an extension and therefore ignored, if you want to be able to read along with the dot and the rest of the string I recommend using a regex in your Request mapping such as: /{userId:.+}
more detailed explanation can be found here: Spring MVC #PathVariable with dot (.) is getting truncated
I'm new to web api. When I'm sending json string of 10MB, the method parameter is showing Null. if I reduce the json string size, then the parameter is showing the string, which I actually sending in Http body. When I googled, I found maxAllowedContentLength property's default value is 30000000 bytes. But my string size is far smaller than this value. Why my http post method is not taking large string as Parameter? How to solve this problem?
Instead of using Parameter for Post method, read input from http content as follows:
var msg = Request.Content.ReadAsStringAsync();
var msgResult = msg.Result;
string reqString = msgResult.ToString();
1)When exactly we need to use HTTPContext (exclusively), please elaborate with an example if possible.
2)What does the following code do
internal string QueryString( string name )
{
if ( HttpContext.Current.Request.QueryString[name] == null )
return string.Empty;
else {
return HttpContext.Current.Request.QueryString[name].ToString();
}
3)Is there any other alternative way where we avoid using HttpContext
Q1:
HttpContext is an object which encapsulates all HTTP-specific information about an individual HTTP request. So, if you need to get any information about receiving HTTP request (for example in order to get the query string parameter values, referral urls, User's IP address etc), you need to use HttpContext object. Further more, this object contains information of current Request, Response, Server, Session, Cache, User and etc.
More information
Usage and examples
Get current session Id: HttpContext.Session.SessionID
Get Timestamp of current request: HttpContext.Timestamp.ToString()
Q2:
In your code sample, you are trying get the value of query string parameter 'name'.
Your dummy request Url looks like - http://www.yourdomain.com/something?name=queryStringValue
QueryString parameter = name
QueryString parameter value = queryStringValue
Hope this helps.
did we need multiple request-dispatcher to send multiple value to the same page
I have written this.
String name=rs.getString("itemname");
String code=rs.getString("itemcode");
String lpr=rs.getString("lastpurchase");
String ur=rs.getString("unitrate");
String pq=rs.getString("pquantity");
String cpq=rs.getString("costpquan");
ServletContext context= getServletContext();
RequestDispatcher rd=context.getRequestDispatcher("/index.jsp")
rd.forward(request,response);
I need to send all these variables to a same page.
No, you don't need multiple dispatchers. You simply need to store each value in a separate request attribute. A better option would be to create an object (Item, for example) containing all these values, and to store this object in a single request attribute.
Item item = new Item(name, code, lpr, ur, pq, cpq);
request.setAttribute("item", item);
rd.forward(request,response);
You should also use much better names for your variables.
Why does my action method only bind the first word of a string I pass into it using a query string?
For example, in jquery, I build a queryString from the results of an ajax call:
success: return(resultData){
var queryString = "?ok=true&message=" + resultData.message;
}
Then I try to load a view into a dialog by calling a controller and passing the queryString
$dialogHandle.load("/Account/RegisterStatus" + queryString, function() { ... });
At this point the queryString correctly hold an entire message. However if I break in my controller:
public ActionResult RegisterStatus(bool ok, string message)
{
//break here
}
I notice that ok binds correctly but message only contains the first word of the error message passed in.
How can I pass a sentence as one string parameter?
Is there a better way to do this, without query string?
EDIT: hmm now that I think about it does make sense since urls cant have space but then how do I accomplish this... is there a specific word delimiter in the default model binder?
It's all about URL escaping: escape("It's me!") // result: It%27s%20me%21
Do that around your resultData.Message and it should work better. For debugging purposes, use Fiddler2 or some Web Inspector to see what request is being send. This is really valuable when you are debugging AJAX...
And of course, do the reverse in C#: HttpUtility.UrDecode Method (String)