Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, September 18, 2010 5:18 AM
Hi!
I have alot of query strings on one of my seartch pages. The problem is that even if the corsponding setting is not set by the enduser it will be visible in the URL. Is there a way to avoid this?
In this case the Controller actions take an object of the following class :
public class AdList
{
public AdList()
{
this.Page = 1;
this.ModelViewAd = new PagedList<ModelViewAdList>();
this.CS = new CategorySelect();
this.LS = new LocationSelect();
}
public PagedList<ModelViewAdList> ModelViewAd { get; set; }
/// <summary>
/// SeartchString
/// </summary>
[DisplayName("Sökord")]
public string S { get; set; }
/// <summary>
/// CategorySelection
/// </summary>
public CategorySelect CS { get; set; }
public LocationSelect LS { get; set; }
/// <summary>
/// Paging
/// </summary>
public int Page { get; set; }
[DisplayName("Säljes")]
public Boolean O1 { get; set; }
[DisplayName("Köpes")]
public Boolean O2 { get; set; }
[DisplayName("Bytes")]
public Boolean O3 { get; set; }
[DisplayName("Uthyres")]
public Boolean O4 { get; set; }
[DisplayName("Önskas Hyra")]
public Boolean O5 { get; set; }
public void SetListOfAds(List<Ad> adList)
{
ModelViewAdList modelViewAdList;
if(adList != null)
{
if(ModelViewAd == null)
ModelViewAd = new PagedList<ModelViewAdList>();
else
ModelViewAd.Clear();
foreach(Ad ad in adList)
{
modelViewAdList = new ModelViewAdList();
modelViewAdList.ModelViewObjectToModel(ad);
ModelViewAd.Add(modelViewAdList);
}
}
}
}
Is it possible to avoid setting all the values in the URL when it is the default valus that are used?
BestRegards
SnowJim
All replies (7)
Friday, October 1, 2010 2:07 AM ✅Answered
Modify this to contain your paramters and add to your Global.asax file.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContextWrapper ht = new HttpContextWrapper(HttpContext.Current);
string inboundUrl = (ht.Request.Url.Scheme + "://" + ht.Request.Url.Authority + ht.Request.Url.AbsolutePath);
// Removing a QueryString parameter if value is default example
// Our example querystring will contain the following: '?p1=default&p2=value&p3=value&p4=value'
// The result of this operation should be to clear the Response and redirect the user to the same url
// without the p1 variable defined in the querystring
//
// CAUTION: this adds more overhead in the processing pipeline
if (ht.Request.QueryString.AllKeys.Contains("p1") && (ht.Request.QueryString["p1"] == "default"))
{
var newQueryString = new System.Collections.Specialized.NameValueCollection();
newQueryString.Add(HttpUtility.ParseQueryString(ht.Request.Url.Query));
newQueryString.Remove("p1");
string qString = string.Empty;
for (int i = 0; i < newQueryString.Count; i++)
qString += string.Format("{0}{1}={2}", (i == 0 ? "?" : "&"), newQueryString.Keys[i], newQueryString[i]);
Response.Redirect(inboundUrl + qString);
}
}
You should probably also test to see if the URL is for the proper route. I'll leave that part to you, but this should help point you in the right direction:
RouteTable.Routes.GetRouteData(ht).Values["controller"].ToString();
Saturday, September 18, 2010 6:21 AM
Hi,
Please refer following
http://stackoverflow.com/questions/1493456/asp-net-hide-querystring-in-url
http://stackoverflow.com/questions/2870013/asp-net-mvc-returning-a-view-with-querystring-intact
hope this helps
Thursday, September 23, 2010 11:11 PM
Hi SnowJim ,
From your description, I think you want to hide the querystring from the url.
As far as I know, you can try to use the hidden field. that's to say, use the javascript to splice all query strings to one hidden field, then you can get the value of the hidden field from the post content.
Or try to use the ajax requesting for passing that query strings.
I hope it is helpful to you.
Friday, September 24, 2010 2:24 AM
Hi!
Sorry that I was not clear.
I do not want to hide the querystring, its vary important that a saved adress takes the user to the exact same page as when the adress was saved. The problem is if the user do not set all the filters the default values will be placed in the querystring no mather what? I know that I can set default values on routs in MVC but this does not involve querystring if I understand this right?
If O1 is set to the default value false the querystring will be filled upp with someadress?O1=false even if its not needed?
BestRegards
Wednesday, September 29, 2010 5:30 AM
Okay, I will try to explain it even simpler.
Say that you got the following querystrings : Page, Post
To get to page = 5 and post 10 we need an url like this : someadr.com?page=5&post=10
Now Say that we only want to go to page 5 and to the first post. the defailt value of post is 0 so the url should look like this : someadr.com?page=5
In MVC all the querystrings will be transfared even when the value is set to the default value, this generats a unnecessary long url. I know that if I used routs (someadr.com/5/10) it would be simple to set default value in the rout but how to do this with querystrings?
BestRegards
Wednesday, September 29, 2010 10:29 PM
<base href="/" mce_href="/"> <link rel="stylesheet" href="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/themes/advanced/skins/default/content.css" mce_href="/tiny_mce/jscripts/tiny_mce/themes/advanced/skins/default/content.css">
Hi SnowJim ,
You can try to add the querystring at the end of the url, like web.com/controller/action?name1=name&name2=nam
then you can create action method via Request class for getting the parameters, or use the action method parameters directly.
I hope it is helpful to you.
Thursday, September 30, 2010 2:08 AM
Hi,
Okay, but where do I set the default values? I supose that you menar that the inparameters to the servirside code should have the querystring parameters as inparam? This is what I already have. The problem is that I do not know how to explain to the client side that it dont need to send the querystrings that is not set becourse the service site will use the default values.
BestRegards