Is there any easy way to extract the parameters of the referrer url as contained in Request.UrlReferrer? Is there another way to get the parameters used by the referrer?
Query?blahID=3&name=blah
I am refering to getting blahID and name from the url. It can be done with a bunch of string manipulations, but was hoping there was an easier way.
Use HttpUtility.ParseQueryString from System.Web. Something like this should work:
string blahID = string.Empty;
if(Request.UrlReferrer != null)
{
var q = HttpUtility.ParseQueryString(Request.UrlReferrer.Query);
blahID = q["blahID"];
}
Related
According to the documentation, it needs to follows the Form Post rules at: https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4. When looking at that information it did not give me much to work with in terms of complex objects or maps.
Right now, If I have a list for example: Each item in the list needs to be stringified.
var params = {"list": [1,2,3]};
// needs to be stringed.
params["list"] = params["list"].map((item)=>item.toString()).toList();
Simple. Also all base items need to be a string as well
var params = {"number": 1, "boolean": true};
params = params.forEach((k,v)=> params[k].toString());
But how do we handle maps?
var params = {"map": {"a":1,"b":"foo","c":false,"d":[]}};
// ??
It seems that after testing in my app and in dart pad, you need to make sure everything is strings, so i am trying to come up with a way to effectively cover lists, maps, and maybe more complex objects for encoding.
var params = {};
params["list"] = [1,2,3];
params["number"] = 1;
params["boolean"] = true;
params["map"] = {"a":1,"b":"foo","c":false,"d":[]};
params.forEach((String key, dynamic value){
if(value is List){
params[key] = value.map((v)=>v.toString()).toList();
}else if(value is Map){
// ????
}else{
params[key] = value.toString();
}
//maybe have an additional one for custom classes, but if they are being passed around they should already have their own JSON Parsing implementations.
}
Ideally, the result of this would be passed into:
Uri myUri = new Uri(queryParameters: params);
and right now, while i solved the list issue, it doesn't like receiving maps. Part of me just wanted to stringify the map as a whole, but i wasn't not sure if there was a better way. I know that when someone accidentally stringified the array, it was not giving me: ?id=1&id=2 but instead ?id=%5B1%2C2%5D which was not correct.
I don't think there is any special support for maps. Query parameters itself is a map from string to string or string to list-of-strings.
Everything else need to be brought into this format first before you can pass it as query parameter.
A simple approach would be to JSON encode the map and pass the resulting string as a single query parameter.
I am trying to setup a search in umbraco examine.I have two search fields ,material and manufacturer.when I trying to search with one material and one manufactuere it will give the correct result.but when try to search more than one material or manufacturer it doesn't give the result.here is my code
const string materialSearchFields = "material";
const string manufacturerSearchFields = "manufacturer";
if (!string.IsNullOrEmpty(Request.QueryString["material"]))
{
material = Helper.StripTags(Request.QueryString["material"]);
}
if (!string.IsNullOrEmpty(Request.QueryString["manufacturer"]))
{
manufacturer = Helper.StripTags(Request.QueryString["manufacturer"]);
}
if (!string.IsNullOrEmpty(Request.QueryString["material"]) || !string.IsNullOrEmpty(Request.QueryString["manufacturer"]))
{
var query = userFieldSearchCriteria.Field(materialSearchFields, material).And().Field(manufacturerSearchFields, manufacturer).Compile();
contentResults = contentSearcher.Search(query).ToList();
}
here my search keywors in querystring is material=iron,steel
how can we split this keyword and search done?
Thanks in advance for the help....
You are using the AND operator, in your case I think you are looking for the GROUPEDOR instead?
I was just working in an old project and grabbed this snipet from there (which I've adapted for your needs). I think it's going to help you:
public IEnumerable<DynamicNode> SearchUmbraco(string[] keywords, string currentCulture)
{
// In this case I had some diferent cultures, so this sets the BaseSearchProvider to the given culture parameter. You might not need this, use your default one.
BaseSearchProvider searcher = SetBaseSearchProvider(currentCulture);
var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
var groupedQuery = searchCriteria.GroupedOr(new[] {"manufacturer", "material"}, keywords).Compile();
var searchResults = searcher.Search(groupedQuery);
// ... return IEnumerable of dynamic nodes (in this snipet case)
}
I just split(etc) the keywords in an helper and pass them to a string array when I call this method.
Just check this infomation on the umbraco blog: http://umbraco.com/follow-us/blog-archive/2011/9/16/examining-examine.aspx
I am calling a controller method using Url.action like,
location.href = '#Url.Action("Display", "Customer", new { username = "abc",name = "abcdef",country = "India",email = "abc#hmail.com",phone = "9456974545"})';
My controller method is,
public void Display(string username, string name, string country, string email, string phone)
{ }
In this method, I can get only the value of first parameter (username). Its not getting other parameter values that is passed. All other values are null.
Please suggest me, whats wrong?
By default every content (which is not IHtmlString) emitted using a # block is automatically HTML encoded by Razor.
So, #Url.Action() is also get encoded and you are getting plain text. And & is encoded as &
If you dont want to Encode then you should use #Html.Raw(#Url.Action("","")).
The answer for you question is :
location.href = '#Html.Raw(#Url.Action("Display", "Customer", new { username = "abc",name = "abcdef",country = "India",email = "abc#hmail.com",phone = "9456974545"}))';
Hope this helps
There is a problem with '&' being encoded to the '& amp;'
model binder doesnt recognise this value. You need to prevent this encoding by rendering link with Html.Raw function.
Use '#Html.Raw(Url.Action......)'
I have a view:
#{
RouteValueDictionary tRVD = new RouteValueDictionary(ViewContext.RouteData.Values);
foreach (string key in sQS.Split('&'))
{
var itemArr = key.Split('=');
tRVD[itemArr[0]] = itemArr[1];
}
tRVD["dtLastRequest"] = item.dtLastRequest;
tRVD["uiSubscription"] = item.uiSubscription;
tRVD["area"] = "";
}
#Html.Action("Count", "Search", tRVD)
Which generate RouteValueDictionary with 15 keys & values, and then pass it to Action "Count", controller "Search", as i could think?!
I dont know previous programmer, who wrote this code, but controller's action code is full or work with Request.QueryString, but right now, QueryString is empty.
Is it really possible to pass tRVD to Action "Count" as QueryString? Now, i could get values from:
RouteData.Values
there are my keys and values, but i dont want to change code of previous coder and rewrite all from QueryString to RouteData.
The query string is empty because the Count action method is really invoked not using an ordinary request, specifying the action's parameters in the URL, but directly within the framework, carrying the parameters in the RouteData object.
So you should access the parameters using RouteData, as you wrote.
Is there anybody who has written a universal action for iterating through all params values and setting these values on an object?
I want to write something like this:
def updateSomeObject = {obj->
for (def key : params.keySet()) {
if (obj.hasProperty(key) != null) {
def strValue = params[key]
obj[key] = strValue
}
}
but this works only for String values. In my case there are one to one associations, so it has to work with objects too.
I would like not to set properties (their names) to object, which values are null.
I use this to loop grails params:
Collection<?> keys = params.keySet()
for (Object key : keys) {
//check if key=action and key=controller which is grails default params
if (!key.equals("action") && !key.equals("controller")) {
println key //print out params-name
println params.get(key) //print out params-value
}
}
Hope that help...
It looks like you're trying to bind request parameters to an object. You really shouldn't need to write your own code to do this, as the Grails controllers provide a bindData method that does this already.
Is this what you want to do?
obj.properties = params
Hope that helps