Url mapping in web forms - allow question marks and hyphens - c#

I have implemented URL mapping in our ASP.NET 4 application, but I have a problem with some of our content.
Some of our products has a hyphen "-" or a question mark "?" in them. It's not an option to remove that. A productname could be "My Product - Good for you?".
We use two custom made methods, MakeUrlSeoReady and MakeUrlNonSeoReady. We replace space like this: Replace(" ","-"), as this is the most SEO-friendly solution. However, we also need to make this work with both question marks and hyphens.
The reason we use the MakeUrlSeoReady / NonReady methods is to be able to show the "real" name.
Currently the mapping is defined as follows:
routes.MapPageRoute("Produkt visning",
"artikler/{Categoryname}/{SubCategoryname}/{ProductName}",
"~/SingleProduct.aspx");
So what I do is I retrieve the product depending on the ProductName. I use two methods I've created:
public static string MakeUrlNonSeoReady(string text)
{
return text.ToLower().
Replace("oe", "ø").
Replace("aa", "å").
Replace("ae", "æ").
Replace("-", " ");
}
public static string MakeUrlSeoReady(string text)
{
return text.ToLower().
Replace("ø", "oe").
Replace("å", "aa").
Replace("æ", "ae").
Replace(" ", "-");
}
So in the SingleProduct.aspx page I use the following string to get from our database:
string categoryName = HelperFunctions.MakeUrlNonSeoReady(Page.RouteData.Values["ProductName"]);
But this will of course not work. So any help is really appreciated :-)

An arguably cleaner and simpler method is to use a unique product identifier that is numerical or alphanumerical and is natively HTML encoded, and then simply put the product name as an unused parameter for SEO or search purposes.
MSDN RouteCollection.MapPageRoute Method (String, String, String, Boolean, RouteValueDictionary)
routes.MapPageRoute("Produkt visning",
"artikler/{Categoryname}/{SubCategoryname}/{ProductIdentifier}/{ProductName}",
"~/SingleProduct.aspx", false, new RouteValueDictionary
{ { "ProductName ", string.Empty } });

Related

String format proper using

Do you know another more proper way to do the same things ?
string initialTemplate = "{0}-{1}";
string template = string.Format(initialTemplate, "first", "{0}");
string answer = string.Format(template, "second");
Also the following way has actually known, but in my current case unfortunatelyI can't use that method(i think that that way more proper and the logic more clear):
string initialTemplate = "{0}-{{0}}";
string template = string.Format(initialTemplate, "first");
string answer = string.Format(template, "second");
Maybe is there another hint how to do that?
UPDATE
I'm so sorry but from yours answers I've learnt that my question wasn't enough clear. So I've added a little bit more description.
My situation:
//that template is actually placed in *.resx file
//I want storing only one template and use that in different situations
public const string InitialTemplate = "{0}-{1}";
public static string GetMessage(string one, string two)
{
return string.Format(InitialTemplate, one, two);
}
public static string GetTemplate(string one)
{
return string.Format(InitialTemplate, one, "{0}");
}
//or morew universal way
public static string GetTemplate(params object[] args)
{
return string.Format(InitialTemplate, args, "{0}");
}
static void Main(string[] args)
{
//in almost all cases in my project i need to use string.format like this
string message = GetMessage("one", "two");
//but there is another way where i have to use
//the template have already been assigned first argument
//the result must be "one-{0}"
string getTemplateWithAssignedFirstArg = GetTemplate("one");
}
Do you know more proper way for that kind of situation ?
If you are using C# 6 you can also use string interpolation.
https://msdn.microsoft.com/en-us/library/dn961160.aspx
var answer = $"{firstVar}-{secondVar}";
string initialTemplate = "{0}-{1}";
string answer = string.Format(initialTemplate, "first", "second");
Should do the trick. Or cut out the middle man with:
string answer = string.Format("{0}-{1}", "first", "second");
String.Format is a very useful convenience, but I'd be wary of using it to build format strings that you're going to use to create other format strings. Someone trying to maintain that code, figure out what's going on, and perhaps modify it will be baffled. Using String.Format that way is technically possible, and there could even be scenarios where it's useful, but it's probably just going to result in something that works but is very difficult to understand and debug.
My first suggestion would be to use a StringBuilder. Even when you're appending to the StringBuilder you can use String.Format if needed to create the individual strings.
I wonder if perhaps what you describe in the question is taking place across multiple methods (which is why you might be building your format string in steps.) If that's the case, I recommend not building the string in steps like that. Don't actually start building the string until you have all of the data that you need together, and then build the string at once.

How do I format an Enum? [duplicate]

This question already has answers here:
Enum ToString with user friendly strings
(25 answers)
Closed 7 years ago.
I need my enum to return a formatted string to the view, for example:
public enum status
{
NotStarted,
InProgress,
}
return: Not Started and In Progress. How I do it? Thanks (language C#)
enums don't do that. You'd need to provide a map of enum values to display strings or you could do something like define an attribute with a display string that you can use (which requires some fiddly reflection to get the attribute for a given enum value, but has the advantage that you can define the display name right where you define the enum value).
For example, you can use a Dictionary<status,string> to map them:
var myMap = new Dictionary<status,string>()
{
{ status.NotStarted, "Not Started" },
{ status.InProgress, "In Progress" }
};
Now to get the display string for a given status value s you'd just do something like:
var s = status.NotStarted;
var displayString = myMap[s]; // "Not Started"
Of course, you'd put this in a class somewhere so it's only defined once in one place.
Another rather brittle, quick-and-dirty way to do it would be to exploit the fact that your enum names are Pascal-cased and use something like a regex to take the enum name and insert an extra space. But that's pretty hacky. So you could do something like:
var r = new Regex("([A-Z][a-z]*)([A-Z][a-z]*)");
var displayString = r.Replace(s.ToString(),"$1 $2"); // "Not Started"
But that would choke on any enum values that didn't fit the pattern of two Pascal-cased words. Of course, you could make your regex more flexible, but that's beyond the scope of the question.
Calling ToString on an emum value is the equivalent to Enum.GetName which would give you the named value i.e.
Console.WriteLine(status.NotStarted.ToString()) // NotStarted
From there, assuming the format is consistent, you could convert the string from Pascal casing to a whitespace separated string e.g.
string result = Regex.Replace(status.NotStarted, "([a-z])([A-Z])", "$1 $2");
Console.WriteLine(result); // Not Started
See example.
Enum.GetName(typeof (Status), Status.InProgress));

Passing parameters though url?

I am trying to test a call to my one of my functions.
This is called from outside of my website an acts like a Webservice.
To test im trying to pass the parameters though my url.
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]?Id=4785
My code in my service:
public void UploadValuationDetails(Dictionary<string, ValuationDetails> JsonResult, int Id)
{
DatabaseHelper DBH = new DatabaseHelper();
foreach (var item in JsonResult)
{ //(ValuationId , TagName , TagValue , ImageBase64)
DBH.WSValuationDetailUpdate(Id, item.Key, item.Value.TagValue, item.Value.ImageBase64);
}
}
ValuationDetails class:
public class ValuationDetails
{
public string TagValue { get; set; }
public string ImageBase64 { get; set; }
}
Edit Changed ? for the second parameter to &:
> http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails={'property_details_address_address1':[{TagValue:'Test',ImageBase64:''}]}&Id=4785
After changing my url to the one above a break point was hit but the values were incorrect.
Edit 2 Trying to get the correct values in the json result.
I think i'm closer:
http://localhost:0000/APIService/UploadValuationDetails?JsonResult={TagName:"property_details_address_address1",ValuationDetails:{TagValue:"Test","ImageBase64:""}}]&Id=4785
But now my jsonResult = 0
You should use an ampersand (&) to separate multiple query string parameters. As you have it, you're using ?, so "?Id=4785" is being interpreted as part of the value for the ValuationDetails parameter.
Corrected:
this is correct ┐
↓
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=
[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]&Id=4785
↑
but this should be fixed ┘
I think it is better to Encode the JSON too.
Since the moment you will have for instance in side your data an ? or & you will get an exception too.
Your URL string looks improperly formatted.
For the separator of the URL and the parameters you would use ?.
But to separate parameters use &
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]&Id=4785
Your JSON is invalid.
I've worked with it a bit, but it still needs input from you.
[
{
"property_details_address_address1": {
"TagValue": "Test"
},
"needs_a_name_here": {
"ImageBase64": ""
}
}
]
Notice that i've put quotes around the names. And your second object also requires a name.
I used JSONLint to validate and create the proper json

words stemmer class c#

I am trying the following stemming class :
static class StemmerSteps
{
public static string stepSufixremover(this string str, string suffex)
{
if (str.EndsWith(suffex))
{
................
}
return str;
}
public static string stepPrefixemover(this string str, string prefix)
{
if (str.StartsWith(prefix)
{
.....................
}
return str;
}
}
this class works with one prefix or suffix. is there any suggestion to allow a list of prefixes or suffixes to go through the class and compare against each (str). your kind action really appreciated.
Instead of creating your own class from scratch (unless this is homework) I would definitive use an existing library. This answer provides an example of code that that implements the Porter Stemming Algorithm:
https://stackoverflow.com/questions/7611455/how-to-perform-stemming-in-c
Put your suffix/prefixes in a collection (like a List<>), and loop through and apply each possible one. This collection would need to be passed into the method.
List<string> suffixes = ...;
for (suffix in suffixes)
if (str.EndsWith(suffix))
str = str.Remove(str.Length - suffix.Length, suffix.Length);
EDIT
Considering your comment:
"just want to look if the string starts-/endswith any of the passed strings"
may be something like this can fit your needs:
public static string stepSufixremover(this string str, IEnumerable<string> suffex)
{
string suf = suffex.Where(x=>str.EndsWith(x)).SingleOrDefault();
if(!string.IsNullOrEmpty(suf))
{
str = str.Remove(str.Length - suf.Length, suf.Length);
}
return str;
}
If you use this like:
"hello".stepone(new string[]{"lo","l"}).Dump();
it produces:
hel
The simplest code would involve regular expressions.
For example, this would identify some English suffixes:
'^(.*?)(ing|ly|ed|ious|ies|ive|es|s|ment)?$'
One problem is that stemming is not as accurate as lemmatization. Lematization would require POS tagging for accuracy. For example, you don't want to add an -ing suffix to dove if it's a noun.
Another problem is that some suffixes also require prefixes. For example, you must add en- to -rich- to add a -ment suffix in en-rich-ment -- unlike a root like -govern- where you can add the suffix without any prefix.

Anyway to make a IList.Contains() act more like a wildcard contains?

I am trying to parse thru a csv string, put the results into a IList collection and then trying to find a way to do a wildcard 'contains' based on what was passed in. Right now I have the following:
public static IList<string> DBExclusionList
{
get
{
Regex splitRx = new Regex(#",\s*", RegexOptions.Compiled);
String list = (string)_asr.GetValue("DBExclusionList",typeof(string));
string[] fields = splitRx.Split(list);
return fields;
}
}
if (DBExclusionList.Contains(dbx.Name.ToString())==false)
{...}
So if the string I am parsing (key value from .config file) contains:
key="DBExclusionList" value="ReportServer,ReportServerTempDB,SQLSentry20,_TEST"
The DBExclusionList.Contains() works very well for exact matches on the first 3 items in the list, but I want to be able to ALSO have it for any partial match of the fourth item '_TEST'
is there any way to do it? I could certainly hardcode it to always exclude whatever but I'd rather not.
thanks.
Using Linq :
if (DBExclusionList.Any(s => s.Contains(dbx.Name.ToString())))
Since you're using .NET 3.5, you could use the Where() LINQ extension method:
DBExclusionList.Where(item => item.Contains(dbx.Name.ToString()))
Don't use regex to split. Use string.Split() and delimit your _asr.GetValue("DBExclusionList) with something like semi-colons.
Each item in _asr.GetValue("DBExclusionList) should be a regex pattern, so you check against each one in turn.

Categories

Resources