views:

49

answers:

1

In my global.asax.cs file. I add an entry

       routes.MapRoute(
            "Static text",
            "Static/General/{filePath}",
            new { controller = "Static", Action = "General", filePath = "" },
          //  new { filePath = @"xxxx" } // greedy regular expression
        );

What I want to do is to take the content from the static files and insert into my view pages. This map works fine if my filePath is in the root such as 1.txt. But it won't work if the file is located in some sub directory such as staticfiles/1.txt. Because the routing module will consider "staticfiles" as the filePath and leave the "1.txt" as some other parameter. I know what I need to do is applying some regular expression trick on the filePath parameter. But I couldn't figure out how to make the regex engine to read all the way to end of url. Can someone show me the trick I should apply? Thanks very much.

+1  A: 

Try putting an asterix before filePath:

routes.MapRoute(
    "Static text",
    "Static/General/{*filePath}",
    new { controller = "Static", Action = "General", filePath = "" }
);
Darin Dimitrov
Tested and didn't work. My controller received null as filePath value.
Wei Ma
Did you put this **before** the `Default` route?
Darin Dimitrov
Yes, I did put this before the default route. This is my second last route and the default route is the last one.
Wei Ma
I turned on phil haack's route debugger and this is the result url: http://localhost:4789/static/general/index.htmFalse Static/General/{*filePath} True {controller}/{action}/{id}... omitted other info
Wei Ma
I checked my code again and found that I have uncommented the // new { filePath = @"xxxx" } // greedy regular expression part. Stupid me! I removed the line and now your method works very well. Thank you very much.
Wei Ma