Multiple Optional Parameters with ServiceStack.Net - c#

I'm trying to implement a service with Multiple Optional Parameters using ServiceStack.Net
At the moment my route looks like this
Routes.Add<SaveWeek>("/save/{Year}/{Week}");
I want to support uris like this:
/save/2010/12/Monday/4/Tuesday/6/Wednesday/7
ie Monday=4, Tuesday=6 and Wednesday=7
However I want the ability to ignore days i.e. the person calling the service can decide if they want to save each value for each day...
i.e. Like this with missing parameter values
?Monday=4&Wednesday=7&Friday=6
Of course one solution would be to have the following route and just pass 0 when I don't want to save the value.
Routes.Add<SaveWeek>("/save/{Year}/{Week}/{Monday}/{Tuesday}}/{Weds}/{Thurs}/{Fri}/{Sat}/{Sun}");
But..... is there a better way of achieving this functionality?

When your Route requirements start to get too complicated it will eventually become easier just to add a wild card path so you can parse the rest of the querystring yourself. i.e. in this case since the first part of the querystring remains constant you can add a wild card mapping to store the variable parts of the querystring, i.e:
Routes.Add("/save/{Year}/{Week}/{DaysString*}");
ServiceStack will still populate the partial DTO with the Year and Week fields (as well any fields that were passed in the querystring). The remaining variable parts of the url is stored in the DaysString which you are then free to parse yourself manually. So the above mapping will be able to match urls like:
/save/2010/12/Monday/4/Tuesday/6?Wednesday=7
And populate the following variables in your Request DTO:
Year: 2010
Week: 12
Wednesday: 7
DaysString: Monday/4/Tuesday/6

Related

Saving SSRS multi select parameter value with wildcard

I have searched and searched and have not been able to find the answer to this. I'm no stranger to SSRS, .Net (c# and vb.net), SQL, etc...been in it for years. I currently have a multi-select report parameter that is populated by a dataset in my report. There are hundreds of entries, so I built it to be driven by a wildcard character in a preceding parameter. Everything works fine right now. My question is this: is it possible to enter a wildcard value, select one (or more) of the filtered values and then store that/those value(s) on selection so that a user can go back and enter another wildcard value and select from a newly filtered list? (Basically, remember what has been selected in the overall dataset before report execution and create some sort of comma-separated list as the final parameter value to be passed to the report) I realize this may be better served in a web app w/a reportviewer control, but I'm trying to avoid deviating from the native SSRS server if possible. Thanks in advance!
The way I might approach this (not actually done it but the theory sounds ok)
Have 2 parameters for user input, your current one and a hidden one called say #filter (visible) and #filterHistory (this is the hidden one)
Have a textbox (formatted like button) with something like "Refine" as the text. Set the action to call your report again but set the #filterHistory to be something like #filterHistory & ", " & #filter. Basically we keep appending the last user input to the history.
Then your report would filter based on both parameters. You'll have to do some parsing of the delimited parameter now to split it out into the constituent parts but you get the idea.
I've no time to build a test report but hopefully that will point you in the right direction. If it doesn't help or work then comment and I'll see if I can knock up a quick example.

ASP.NET MVC / C# - String to valid URL characters?

I don't know how to ask this, and I don't know what it is called either so I'll just describe what I want to achieve.
In the database, some articles' title originaly has spaces:
my title with spaces
But in the url, spaces are replaced by other characters such as plus sign (+) or underscore (_)
http://www.mydomain.com/mycontroller/myaction/my_title_with_spaces
or
http://www.mydomain.com/mycontroller/myaction/my+title+with+spaces
Now, how do you do that in C#? Or is there any helper in ASP.NET MVC that can do something like that?
Let say we achieved the said URL, is there any risk that two unique titles become the same in the URL? Please consider these titles:
Title's
Titles
after parsing, they became the same
Titles
Titles
This will be a problem when retrieving the article from the database since I'll get two results, one for "Title" and one for "Title's".
I would implement that functionality like this:
1. When creating a new article, generate the URL representation based on the title.
Use a function that converts the title for a suitable representation.
For example, the title "This is an example" might generate something like "This_is_an_example".
This is up to you. You can create a function that parses the title with rules you define, or use an existing one if it suits better your problem.
2. Ensure the URL representation is unique
If it's going to be an ID, it must be unique. So, when creating new articles you must query your database for the resulting URL representation. If you get a result from the database, it means the newly created article generated the same representation as one of the already created articles. Add something to it so it remains unique.
This could be something like "This_is_an_example_2". In this case, we added the "_2" to the end of the generated representation so it differs from the already existing one. Once more, with each change you have to ensure this representation remains unique.
3. Save the created ID in the database, along with the article data
In the database be sure to save the "This_is_an_example" ID and relate it to the article. Maybe even as the table primary key?
4. Query the database for the correct article
Now, about showing a site visitor the correct article:
When a visitor asks for the following resource, for example:
http://www.mydomain.com/mycontroller/myaction/this_is_an_example_2
Extract the URL part that identifies the article, in this case "this_is_an_example_2".
When you have that, you have the identifier of the article in the database. So, you can query the database for the article with the "this_is_an_example_2" ID and show the article's content to the user.
This might involve some URL rewriting. Unfortunately I'm unable to help you with that in asp.NET. Some search on the subject will surely help you.

Help with MVC 3 Routes

I have a simple route structure in my MVC 3 app that is breaking in an unexpected way.
My URL structure is fairly simple, but contains a handful of variables.
http://site.com/{location}/{stage}/{controller}/{action}/{id}
examples:
http://site.com/ny/prod/server/list - list all prod servers in ny
http://site.com/ny/test/server/123456 - list the details for the server in ny, in the test stage, with id 123456
http://site.com/ny/prod/server/reboot/565656 - reboot the server in ny, in the prod stage, with id 565656
I created the following route in my Global.asax file.
routes.MapRoute("Default", "{location}/{stage}/{controller}/{action}/{id}", new {controller="server", action="list", id = UrlParameter.Optional});
This works fine for displaying a list of servers and the details of a server at /server/details/id, but when I try to execute a reboot, I get an error.
URL: http://site.com/ny/prod/server/reboot/565656
The view 'ny' or its master was not found or no view engine supports
the searched locations. The following locations were searched: ...
Why would it try to look for a view name ny.cshtml?
i think your problem is that you are either not using a constraint to define what location and stage should look like and it is giving you false positives and reading things in where they are not supposed to or you have your route definitions in the wrong order
make sure you have the default mvc defined last and if you have multiple custom routes constrain the either using a regex or custom constraint class to define what locations are valid and they should look like
eg http://site.com/ny/test/server/123456
is ny a valid location - make a custom constraint that defines what a
valid location is validate it against a database or a list of valid
locations
is test a valid stage - regex could be sufficient but i always try to avoid regex whenever possible as it is evil and hard to maintain. again i would write a custom constraint to define what stages are valid likely validating against a list is sufficient is the case as you shouldnt have very many stages
also to be noted with using stages the way you are in your url you can also add authentication rules in a constraint so that for exaple only people that are ..say.. admin or stakeholder roles be mached to the route and regular or non authenticated users would simply fall through to the next route or can simply give a 404
writing routes can be tricky so it is advised to contrain your input data as much as you can especially if you are accepting string data
stephen walther has a good post on writing route constraint at his blog

Can I pass a .net Object via querystring?

I stucked at a condition , where i need to share values between the pages. I want to share value from Codebehind via little or no javascript. I already have a question here on SO , but using JS. Still did'nt got any result so another approach i am asking.
So I want to know can i pass any .net object in query string. SO that i can unbox it on other end conveniently.
Update
Or is there any JavaScript approach, by passing it to windows modal dialog. or something like that.
What I am doing
What i was doing is that on my parent page load. I am extracting the properties from my class that has values fetched from db. and put it in a Session["mySession"]. Some thing like this.
Session["mySession"] = myClass.myStatus which is List<int>;
Now on one my event that checkbox click event from client side, i am opening a popup. and on its page load, extracting the list and filling the checkbox list on the child page.
Now from here user can modify its selection and close this page. Close is done via a button called save , on which i am iterating through the checked items and again sending it in Session["mySession"].
But the problem is here , when ever i again click on radio button to view the updated values , it displays the previous one. That is , If my total count of list is 3 from the db, and after modification it is 1. After reopening it still displays 3 instead of 1.
Yes, you could but you would have to serialize that value so that it could be encoded as a string. I think a much better approach would be to put the object in session rather than on the URL.
I would so something like this.
var stringNumbers = intNumbers.Select(i => i.ToString()).ToArray();
var qsValue = string.Join(",", stringNumbers);
Request.Redirect("Page.aspx?numbers=" + sqValue);
Keep in mind that if there are too many numbers the query string is not the best option. Also remember that anyone can see the query string so if this data needs to be secure do not use the query string. Keep in mind the suggestions of other posters.
Note
If you are using .NET 4 you can simplify the above code:
var qsValue = string.Join(",", intNumbers);
Make the object serializable and store it in an out-of-process session.
All pages on your web application will then be able to access the object.
you could serialize it and make it printable but you shouldn't
really, you shouldn't
The specification does not dictate a minimum or maximum URL length, but implementation varies by browser and version. For example, Internet Explorer does not support URLs that have more than 2083 characters.[6][7] There is no limit on the number of parameters in a URL; only the raw (as opposed to URL encoded) character length of the URL matters. Web servers may also impose limits on the length of the query string, depending on how the URL and query string is stored. If the URL is too long, the web server fails with the 414 Request-URI Too Long HTTP status code.
I would probably use a cookie to store the object.

ASP.Net MVC - route object id == title - how to deal with duplicates?

Web pages have moved to use URLs like:
//weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
i.e. they include the title of the page in the url rather than having some coded id.
I understand that this is useful for SEO, and also for users in finding the correct page where they wish to type in the url.
I would like to follow this approach, but wonder how best to acheive it, and particularly how to deal with duplicates.
Is a database trigger which creates the url based on the title and adds a numeric incremental suffix to any duplicates the best way to go, and if so what would such a trigger look like?
Instead of having an id based on a title they could use id based on both a date and a title (2007/12/03/asp-net-mvc-framework-part-2-url-routing). So if you don't have articles with the same titles in one day (which isn't too severe restriction) duplicates are eliminated.
In Wordpress at least, the "slug" (as they call it) is generated once from the item's title and stored separately in the database. If two "slugs" collide, it appends -1, -2, etc. to the end. I personally prefer if you add an (optional) field to the submission form to allow people to insert their own—it allows people to specify a shorter URL than my-long-article-is-hard-to-type.
You've got to model this concept in your application. URL generation based on title can be automatic, but it can't be invisible. WordPress (and probably other CMS's, too) do a pretty good job of this -- they'll default a URL based on the information you enter, but the "key" part of the URL is visible and editable to the user, and uniqueness is enforced at the appropriate level (globally, per month, per day -- whatever).
Making URL generation completely invisible will lead to confusing errors for the user, I believe.
You could do the same thing that SO does. That is, the slug is only there as GoogleJuice. These two URLs resolve to the same thing:
ASP.Net MVC - route object id == title - how to deal with duplicates?
ASP.Net MVC - route object id == title - how to deal with duplicates?
So, in the example you gave, if the CMS gave each post a unique numeric identifier (which I suppose is quite likely) then you can include it in the URL:
http://weblogs.asp.net/scottgu/archive/2007/12/03/1234/asp-net-mvc-framework-part-2-url-routing
In this example, the symbol 1234 is the post's identifier.

Categories

Resources