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

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.

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.

How would I implement tags within a database?

I have an app and I want users to be able to add tags to articles (similar to stack overflows tags) but I want it to be dynamic. So far I have it as JSON strings (dont squirm too much) but this has a major shortcoming. Firstly I will show you an example, and then explain my problem.
Say I have an article and it's on bees, so users tag: bees, insect, honey, outdoors.
Then in my article class (entity framework) I have
string AssosciatedTags { get; set; }
which would hold: "[\"bees\", \"insect\",\"honey\"]"
then when I render the article I just do in javascript:
JSON.parse(model.AssosciatedTags);
and do whatever I want ... add/remove tags with ease. Ok, so here's where I didn't really think enough, and now I have I still am rather confused. How can I implement a sorting mechanism? Let's say my users are bee crazy. They love bees. Their cute little black and yellow stripes. So they want to click tags they are interested in and my server returns articles that have those tags assosciated.
But how? If I have lots of articles, it seems a bad idea to parse all tags from JSON to an array, look for this tag and return those articles.
My other consideration is to simple to a lookup for ",tagName," which is pretty filthy.
Is there a standard way or one that is hopefully more optimal.
My other consideration is to simple to a lookup for ",tagName," which is pretty filthy.
Yes, that is filthy. This technique has it's place but not here.
Store the tags in another table with the schema int ArticleID, string TagName. That way you can index TagName and query it efficiently.
This is the standard solution.
If you want to you can keep the JSON string but you need to keep it in sync with the table storing the tags. Preferably remove it, or be very careful.
Don't store your tags as JSON. Parsing them will be a huge slowdown on your database. Split them up into their own table. Something like this:
articles <--- table of articles
tags <--- table of tags
articles_tags <--- join table that associates articles with tags
This is called a Many-to-Many relationship.

Guidelines for max string length for data entry

So I am building a new WPF application and I started on my first data entry screen. What maxlength do you normally set for your string fields? I got caught out in my last app my making some of the fields too short.
The string fields will be persisted to a sql server compact db via entity framework.
There are some other posts on StackOverlfow discussing this. You might find this one most helpful: List of standard lengths for database fields.
If you know that the field is used for a first name then you know there's no first name that is longer than 200 chars, then don't make it larger than that. As you can see in the post linked above, there's a link to a catalog that suggests first names are in general not longer than 35 chars.
Fields that you don't know what they will contain such as free text (description or such), just make them the maximum size possible. Better safe than sorry!

Using a Bijective Dictionary in a URL shortener

I am creating a new URL shortener and have read that a Bijective function is what is required. So, I found Jon Skeet's BiDictionary (excellent) and wondered how I'd use this within the URL shortener application. Currently, I Base36 encode the database ID column to create my shortened URL and store the full URL into the table. This works fine but I'm lost as to why I need to use a Bijective function? Do I store the values from the database into the Bijective Dictionary? Is what I currently have functional enough? What would the benefits be of using a Bijective Dictionary?
Not really sure that I understand you question fully...
If I understand you correctly you have created a lookup table with a unique ID and a URL.
Your shortened URL is the Base36 encoded ID.
Let's look at the use cases:
Create a shortened URL
means in you implementation check whether you already have that URL in the table (simple, just return the Base36 encoded ID).
Otherwise just create a new entry and return the Base36 encoding of the new ID.
Lookup the full URL
Decode the Base36 value to an ID, lookup the ID in the table and return -if found- the full URL.
So basically you have created a bijective function (a bidirectional 1:1 correspondence) - just something that works in both directions without any loss, thus fully invertible regarding the given URLs in your table. The Base36 encoding/decoding is fully invertible too so that is a bijective function too :-)
The BiDictionary from Jon you mention would be good base for an in-memory-cache (recommend write-through) so you can avoid the DB roundtrip where possible. The Bidictionary uses Dictionary while for a cache which can be accessed by multiple threads I would strongly recommend using ConcurrentDictionary . In your case the List<> part from Jon's implementation is not needed since you always would have a 1:1 correspondence. For faster lookup you could use the Base36 encoded value as a key...

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