locked
Application_BeginRequest and /WebResource.axd in URL Rewrite RRS feed

  • Question

  • User-42864417 posted

    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, 

    Monday, December 18, 2006 9:45 AM

All replies

  • User1347187933 posted

    Hi,

    your url mapping code seems only works for .aspx and files with extension mapped to same HttpHandler.

    1. Only the files with extensions that have been map to aspnet_isapi.dll in your IIS website setting will be processed by ASP.NET.

    here is one image where you can find IIS Website Mappings:

    So your code don't effect to .html .asp files by default. Please also check your index files setting http://siteneme/product/1234/ or http://siteneme/page/4/ and if directory browsing is enable, since file like index.html, default.asp may be select if you don't spefifiy them in your URL.

    2. Further more, even the file extension has been mapped to your ASP.NET ISAPI, only the configured HttpHandler is select to process your code. 

     So your url mapping code seems only works for .aspx

    Tuesday, December 19, 2006 1:02 AM
  • User1603627504 posted

    Hi,

     Thanks  GeorgeZ , I got the same problem but now its cleared.

     Thanks a lot.

    Tuesday, April 24, 2007 8:04 AM