.NET Routing contain case sensitive? - asp.net-core

I was working with routes in .NET Core and I noticed that when I use the same route but passing it with a lowercase letter, I got access to the same page.
Example:
mydomain.com/Account/Login
mydomain.com/account/login
Why was there no distinction between uppercase and lowercase in this case? And I am not using services.AddRouting (options => options.LowercaseUrls = true); to allow this.
I just want to know how that letter distinction works and why it continues to work.

Quote from the Doc :
Text matching is case-insensitive and based on the decoded representation of the URL's path.
And services.AddRouting (options => options.LowercaseUrls = true); is just used to convert the route template to lowercase. But you can still access it with uppercase Url.

Related

Apache httpd server redirect url with anchor tag(fragment id) in it [duplicate]

I'm overhauling a website that someone else built for my organization. It was originally set up with "not so great" anchor links which included spaces. I have replaced those anchors with new ones that will work better.
Example:
One of the old anchors looked like this /course/#To Have which browsers would luckily convert to /course/#To%20Have. I changed that anchor to this: /course/#to-have.
I'm now wanting to make sure that any anchors that may have been shared on social media or that could be linked to from other websites still work; I was planning on doing this via redirect in the .htaccess file, such as this one:
Redirect 301 /course/#To%20Have /course/#to-have
After some research I've found that this is not possible due to the # in the URLs. And I also have not seen examples where an anchor was redirected to another anchor.
Is this possible?
As mentioned in my comment, this is not possible with .htaccess.
Reason being: the hash part (known as the fragment) is not actually sent to the server, and so Apache would not be able to pick it up. Servers may only pick up everything before that, which is described in the Syntax section of this article.
As an alternative, I would recommend that you use JavaScript to convert the fragment before scrolling to its location. You can do that by pulling in the value of [window.]location.hash (the part in square parenthises is optional as location is also available in the global scope) if it exists, as shown below:
if (window.location.hash) {
// Function to 'slugify' the fragment
// #see https://gist.github.com/mathewbyrne/1280286#gistcomment-1606270
var slugify = function(input) {
return input.toString().toLowerCase().trim()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-'); // Replace multiple - with single -
}
// Now get the hash and 'slugify' it
var hash = slugify(window.location.hash.split('#')[1]);
// Go to the new hash by setting it
window.location.hash = '#' + hash;
}

Handling '+' in ASP Net Core routes

How are '+' handled in ASP Net Core routes?
I have a route defined like this:
"/Test/{words}/{type:regex(^(AA|BB|CC)$)}/{search2}"
with parameters of the function being string[] words, string type, string search2
With different URLS:
/Test/word1+word2/AA/blablabla => 404
/Test/word1%20word2/AA/blablabla => Ok, words={"word1 word2"}
/Test/word1,word2/AA/blablabla => Ok, words={"word1","word2"}
I don't understand the 404. any idea why it happens? I would have translated the '+' with a space.
You must encode a space; it is correct to encode a space as +, but only in the query string; in the path you must use %20. For + you can use %2b. (Http 1.1)

Yii2 routing: url-rule not working when the "/index"-action is not explicitly specified

This route need a little extra work in order for it to work properly, but I am not able to see what I need to do:
'http://<caregiverName:\w+>.' . $domain . '/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
When I make call like this controller/action, everything works fine. When I make this call controller (without the explicit index) in order to make call to actionIndex, I am not able to catch caregiverName param. But when I make this call controller/index (index explicitly specified), it works normally.
What I need to rework in the router?
Two things:
Rule for calls without index
You need a second rule with the index as a static value when it is not provided. Add this after your first rule like so:
'http://<caregiverName:[\w-.]+>.' . $domain . '/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'http://<caregiverName:[\w-.]+>.' . $domain . '/<controller:\w+>' => '<controller>/index',
The reason for this is that yii will to this normally, but not for your explicitly defined rule. The reason to put it after is that it would otherwise match before the other and always put you on the index page ;-).
Regex corrections for domain-characters
In you rule you use \w+ as a placeholder for a domain name. the way you specified your rule, it won't work with any other characters than "word-characters" (a-zA-Z0-9_). The dot (subdomain) and the dash (-) are missing! For the domain perfectly.valid-subdomain.com your rule won't work. You can find an axplanation of the regex shorthands here.
Check out my example above how I specified the character classes. You could of course also validate the length etc, but this is not the scope of this answer...you should get the gist.
Tell me if you need more information!

Completely custom path with YII?

I have various products with their own set paths. Eg:
electronics/mp3-players/sony-hg122
fitness/devices/gymboss
If want to be able to access URLs in this format. For example:
http://www.mysite.com/fitness/devices/gymboss
http://www.mysite.com/electronics/mp3-players/sony-hg122
My strategy was to override the "init" function of the SiteController in order to catch the paths and then direct it to my own implementation of a render function. However, this doesn't allow me to catch the path.
Am I going about it the wrong way? What would be the correct strategy to do this?
** EDIT **
I figure I have to make use of the URL manager. But how do I dynamically add path formats if they are all custom in a database?
Eskimo's setup is a good solid approach for most Yii systems. However, for yours, I would suggest creating a custom UrlRule to query your database:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes
Note: the URL rules are parsed on every single Yii request, so be careful in there. If you aren't efficient, you can rapidly slow down your site. By default rules are cached (if you have a cache setup), but I don't know if that applies to dynamic DB rules (I would think not).
In your URL manager (protected/config/main.php), Set urlFormat to path (and toptionally set showScriptName to false (this hides the index.php part of the URL))
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
Next, in your rules, you could setup something like:
catalogue/<category_url:.+>/<product_url:.+> => product/view,
So what this does is route and request with a structure like catalogue/electronics/ipods to the ProductController actionView. You can then access the category_url and product_url portions of the URL like so:
$_GET['category_url'];
$_GET['product_url'];
How this rule works is, any URL which starts with the word catalogue (directly after your domain name) which is followed by another word (category_url), and another word (product_url), will be directed to that controller/action.
You will notice that in my example I am preceding the category and product with the word catalogue. Obviously you could replace this with whatever you prefer or leave it out all together. The reason I have put it in is, consider the following URL:
http://mywebsite.com/site/about
If you left out the 'catalogue' portion of the URL and defined your rule only as:
<category_url:.+>/<product_url:.+> => product/view,
the URL Manager would see the site portion of the URL as the category_url value, and the about portion as the product_url. To prevent this you can either have the catalogue protion of the URL, or define rules for the non catalogue pages (ie; define a rule for site/about)
Rules are interpreted top to bottom, and only the first rule is matched. Obviously you can add as many rules as you need for as many different URL structures as you need.
I hope this gets you on the right path, feel free to comment with any questions or clarifications you need

Rails 3 - Make text field accept only numeric values

How do I make a text field accept only numeric values? If I press a letter or a symbol, the text field should not be filled, it should only allow numbers.
Is there a rails way to do this?
Use number_field_tag, this will generate a HTML5 number field
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/number_field_tag
On the server side validate numericality:
class SomeModel
validates :some_column, :numericality => {:only_integer => true}
end
and on the client side, add an input mask via javascript https://github.com/ruoso/jquery-regex-mask-plugin
$('#some_input').regexMask(/^\d+$/);
#clyfe's answer is good, but that plugin doesn't work with HTML5 type=number elements. Here's some quick jQuery code that only allows integers:
$("input[type=number]").keypress(function(event) {
if (!event.charCode) return true;
ch = String.fromCharCode(event.charCode);
return (/[\d]/.test(ch));
});
to allow decimals or commas, make the regex look more like those in the plugin, e.g. https://github.com/ruoso/jquery-regex-mask-plugin/blob/master/regex-mask-plugin.js#L8 :
/^((\d{1,3}(\,\d{3})*(((\,\d{0,2}))|((\.\d*)?)))|(\d+(\.\d*)?))$/
(Note that different locales have different conventions for decimals and commas, so it's probably safer to just allow digits :-)
Note also that this is a workaround for the Chrome bugs mentioned here:
https://code.google.com/p/chromium/issues/detail?id=304455
https://github.com/angular/angular.js/issues/2144