I would like to use a RegEx to create a new route in ASP.Net MVC 4.
I want to redirect to a specific controller and action when the url contains one letter between slashes and a word:
http://www.myurl.com/a/something/anotherthing/?mykeyword=.....
My current route is:
routes.MapRoute(
"MyRoute",
"{*something}",
new { controller = "mycontroller", action = "myaction" },
new { theregex = #".*/a/.*?mykeyword=.*" });
But I never go into mycontroller/myaction. I'm always redirect to the default route.
Can someone tell me what is wrong in the regex?
Thank you!
You can try:
https?:\/\/[^\/]*\/a\/[^?]*\?mykeyword=
DEMO
String literals for use in programs:
#"https?:\/\/[^\/]*\/a\/[^?]*\?mykeyword="
Related
I am reviewing the configuration of FW/1. I notice that some of the routes start with a / and others don't. Is there any difference between the two?
variables.framework.routes = [
{ "chart/home" = "chart/home"},
...
{ "/location/home" = "location/home"},
Have you noticed any difference in behavior from the two? I don't think there is a difference. From the documentation and examples that I could find they are all preceded by a /. I presume that FW/1 is allowing both but they work the same.
Snippet from the documentation here - http://framework-one.github.io/documentation/developing-applications.html#url-routes:
URL Routes
In addition to the standard /section/item and /module:section/item URLs that FW/1 supports ...
An example from farther down that page shows the standard routes starting with a /:
Here’s an example showing all the features together:
variables.framework.routes = [
{ "/product/:id" = "/product/view/id/:id", "/user/{id:[0-9]+}" = "/user/view/id/:id",
hint = "Display a specific product or user" },
{ "/products" = "/product/list", "/users" = "/user/list" },
{ "/old/url" = "302:/new/url" }
];
Here is a link to the code that processes the routes you define - https://github.com/framework-one/fw1/blob/develop/framework/one.cfc#L1954-L2047
In order to test this theory you could try the following.
Browse to www.yourdomain.com/location/home that should match the second route in your example.
Browse to www.yourdomain.com/chart/home that should match the first route in your example.
Browse to www.yourdomain.com/sometextchart/home does that match the first route in your example?
Browse to www.yourdomain.com/somefolder/chart/home does that match the first route in your example?
Browse to www.yourdomain.com/somefolder/sometextchart/home does that match the first route in your example?
I am trying to use the wildcard match on routes in FW/1 v 3.5.
Application.cfc
component extends="framework.one" {
this.name= "Wildcard7";
variables.framework = {
home = 'main.home',
action = 'fuseaction',
baseURL = 'useCgiScriptName',
trace = isDebugMode()
};
variables.framework.routes = [
{ "main/home" = "main/home"},
{ "*" = "main/404"}
];
}
When I run the page, without specifying an action, I get the main/404 page instead of main/home
** FW/1 trace**
How can I get main/404 to run only on invalid pages?
When I run the page, without specifying an action, I get the main/404 page instead of main/home
I assume you are trying to access the page like so - your.domain/index.cfm/main. Note the lack of the home action.
Based on your routes, your first route is saying if the path supplied equals "main/home" then point to the view main/home. If there is an action of home in a main.cfc controller then that will be ran prior to rendering the view.
Leaving off the action, home, would not match any of your current routes; resulting in your wildcard catching it. You would need to handle it by including another route like {"main" = "main"}.
UPDATE:
To access main/home from your.domain/index.cfm, you can try passing a route of {"/" = "main/home"}. I would suggest this being above your wildcard and below any other routes to avoid any freak matches.
I've been struggling with this problem the last 2 hours. I'm trying to add a regex-route to my Zend Framework (V1) application. My other routes are static and stored in my application.ini file and that is where i want to put some new regex-routes.
Before i continue to work on more complex regex routes i started to translate the following (working) route to a regex route:
resources.router.routes.shift-display.route = /shift/display/:id
resources.router.routes.shift-display.defaults.module = shift
resources.router.routes.shift-display.defaults.controller = index
resources.router.routes.shift-display.defaults.action = display
Here is what i came up with:
resources.router.routes.shift-display.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.shift-display.regex = "shift/display/(\d+)"
resources.router.routes.shift-display.defaults.module = shift
resources.router.routes.shift-display.defaults.controller = index
resources.router.routes.shift-display.defaults.action = display
resources.router.routes.shift-display.map.1 = id
resources.router.routes.shift-display.reverse = "shift/display/%d"
But it's not working. It seems the route isn't recognized by the router. When trying to open e.g. vh.localhost/shift/display/7 i get "Invalid controller specified (display)" so the router uses the default route. I also tried to prepend the regex-route with a slash. I tried to use the url-helper to generate links (with passed ID param) using the new regex-route and it works.
Anyone has a clue?
Sometimes is it just that simple. When definining the route, the mistake was made in that line:
resources.router.routes.shift-display.regex = "shift/display/(\d+)"
It has to be
resources.router.routes.shift-display.route = "shift/display/(\d+)"
4 hours for nothing. But at least I finally found it ;-)
I am trying to route a URL using codeigniter URL routing.
I want to redirect a url like
/users/edit?email to userController/editemail
/users/edit?password to userController/editpassword
I tried using the following line in routes.php in config folder
$route["users/edit?(email|password)"] = "userController/edit$1";
This displays page not found. I am guessing that ? is being treated as a regular expression character. I tried escaping it but that didn't work either.
I don't want to set the config setting uri_protocol to PATH_INFO or QUERY_STRING, since this is just a pretty URL I want to setup, not pass anything to the action.
Can somebody help me out over here?
Regards
You should escape the ? like this, it should work. (not tested)
$route["users/edit\?(email|password)"] = "userController/edit$1";
Later edit:
This works as intended:
$route["users/edit(email|password)?"] = "userController/edit$1";
The userController looks like this
<?php
class UserController extends Controller {
function edit()
{
echo "general edit";
}
function editemail()
{
echo "edit email!";
}
function editpassword()
{
echo "edit password";
}
}
Router works like this:
if you go to http://sitename/index.php/users/editemail you see editemail() action.
if you go to http://sitename/index.php/users/editpassword you see editpassword() action.
if you go to http://sitename/index.php/users/edit you see the edit() action (the question mark makes optional the email/password field and you can do some other things in edit() action
I'm working on WatiN automation tool. I'm having problem in regular expression. I've situation where i have to enter some text and click on a button in the popup window. I'm using AttachToIE method and URL attribute("http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=ef5ad7ef5490-4656-9669-32464aeba7cd") of the popup to attach to the popup.
The problem is each time the popup appears the ID value in the URL changes. So i'm not able to access the popup. can anyone plz help with this by giving me Regular Expression for the changing value of ID in the below URL
("http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=ef5ad7ef5490-4656-9669-32464aeba7cd")
thanking you
It appears that you have a URL with 2 query string parameters Type and ID and your pattern is:
"http://192.168.25.10:215/admin/SelectUsers.aspx?Type=Feedback&ID={some id}"
You can use the Find.ByUrl() attribute constraint method and pass it to AttachToIE() as shown below with the regex for matching that pattern.
string url = "http://192.168.25.10:215/admin/SelectUsers.aspx?Type=Feedback&ID="
Regex regex = new Regex(url + "[a-z0-9]+", RegexOptions.IgnoreCase);
IE ie = IE.AttachToIE(Find.ByUrl(regex));
string baseUrl ="http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID="
Regex urlIE= new Regex(baseUrl + "[\\wd]+", RegexOptions.IgnoreCase);
IE ie = IE.AttachToIE(Find.ByUrl(urlIE);
I'm not familiar with WatiN but it looks like it's runs on .Net so perhaps this might help?
var desiredId = "000000000000-0000-0000-000000000000";
var url = "http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=ef5ad7ef5490-4656-9669-32464aeba7cd&someMoreStuff";
var pattern = #"(?i)(?<=FeedBackId=)[-a-z0-9]+";
var result = Regex.Replace(url, pattern, desiredId);
Console.WriteLine(result);
//Output: http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=000000000000-0000-0000-000000000000&someMoreStuff
The following pattern should have the same affect but is more defensive. It should only match stuff in the query string, it requires the id to be 35 characters and won't match similar parameter names like "PreviousFeedBackId".
var pattern = #"(?i)(?<=\?.*\bFeedBackId=)[-a-z0-9]{35,35}\b";
If you just want to extract the id:
var id = Regex.Match(url, pattern).Value;
Console.WriteLine(id);
//output: ef5ad7ef5490-4656-9669-32464aeba7cd
WatiN has a feature where in we can use the url by neglecting the query string. Below is the code which is working fine for me.
string baseUrl = "http://192.168.25.10:215/admin/SelectUsers.aspx";
IE ie = IE.AttachToIE(Find.ByUrl(baseUrl,true));