Hi all,
I'm doing a url mapping (rewrite) on my website using the event Application_BeginRequest on Global.asax. I test the function in a simple website and working PERFECT but in my website is not working, here is the code:
1 protected void Application_BeginRequest(object sender, EventArgs e)
2 {
3 //Get The website virtual directory name from a variable in <appSetting> section in we.comfig
4 string _svd = ConfigurationManager.AppSettings["SiteVirtualDirectory"].ToString();
5 HttpContext _httpContext = HttpContext.Current;
6 //create a regular expression
7 Regex _strPattern = null;
8 //create the expression by checking if the site runing on a virtual directory or not
9 if (!String.IsNullOrEmpty(_svd))
10 {
11 _strPattern = new Regex("/" + _svd + @"/(?:product|page)/[0-9]+.aspx", RegexOptions.IgnoreCase);
12 }
13 else
14 {
15 _strPattern = new Regex(@"/(?:product|page|word3)/[0-9]+.aspx", RegexOptions.IgnoreCase);
16 }
17 //Match the current url with the expression
18 Match _matches = _strPattern.Match(_httpContext.Request.Path.ToString());
19 //Get the matched string
20 string _fileSlug = _matches.ToString();
21 //Check if the virtual directory name is empty or not then remove it from the matched string
22 if (!String.IsNullOrEmpty(_svd))
23 {
24 _fileSlug = _fileSlug.Replace(String.Format("/{0}/",_svd), "");
25 }
26 //check the string for the url is empty or not
27 if (!String.IsNullOrEmpty(_fileSlug))
28 {
29 //The below code for manage categories like products and pages
30 //and the input url should be like: http://sitename/product/1234/ or http://sitename/page/4/
31 string _cat = String.Empty;
32 foreach (char _char in _fileSlug)
33 {
34 if (_char != '/')
35 {
36 _cat += _char;
37 }
38 else
39 {
40 break;
41 }
42 }
43 if (!String.IsNullOrEmpty(_cat))
44 {
45 switch (_cat.ToLower())
46 {
47 case "product":
48 _fileSlug = _fileSlug.Substring(8);
49 _fileSlug = _fileSlug.Replace("/", "");
50 _httpContext.RewritePath("~/product.aspx?pid=" + _fileSlug);
51 break;
52 case "page":
53 _fileSlug = _fileSlug.Substring(5);
54 _fileSlug = _fileSlug.Replace("/", "");
55 _httpContext.RewritePath("~/default.aspx?pname=" + _fileSlug);
56 break;
57 default:
58 break;
59 }
60 }
61 }
62 }
After running the project and debug the visual studio run about 4 to 5 time on a url /WebResource.axd !! I don't know where he got this url from and sure it didn't match with the regular expression so it did passed it, so every thing is working if I open a normal like like default.aspx or product.aspx
The problem here: If i open a mapped url like http://siteneme/product/1234/ or http://siteneme/page/4/ First I got HTTP 404 error (not found) , Second and the important the visual studio didn't enter the event at all, didn't stop at the breakpoint !!!
Is there any know have an explanation for this
Regards,