Reputation: 1823
This sounds like it should be simple but I can't seem to get it working.
I need to add a route that matches a given file name and extension, regardless of the subdirectory (and including the root of the website)
Possible requests could be in the form
/myfile.xml
/somedirectory/myfile.xml
/any/number/of/directories/myfile.xml
I need to match them all and route to the same controller action.
I have tried using wilcards in the route url e.g.
routes.MapRoute(
"MyFileRoute",
"{*path}/myfile.xml",
new { controller = "MyFileController", action = "GetMyFile" }
);
but I get the following error
A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl
I have no control over the request itself since it comes from a 3rd party flash plugin and seems to look for the file where it thinks the html file is, since I am using MVC this is not a physical location.
Upvotes: 1
Views: 902
Reputation: 105029
I've written such route that supports this kind of catch-all parameters. Actually there can be even many of them as long as other segments are defineable, so we can still distinguish which part goes into which segment.
Code and detailed description can be found on my blog.
Upvotes: 0
Reputation: 1823
OK so I guess when all you have is a hammer...
After spending way too much time trying to get routing or a handler to work, I ended up looking at this in a different way and using the IIS7 Url Rewrite to rewrite requests for myfile.xml
in any path to the actual physical location of the real file.
I could just as easily have routed it to the controller action but the only reason that existed in the first place was to serve the file in this case so was no longer required.
I added this rule to the config file which seems to work well.
<rewrite>
<rules>
<clear />
<rule name="Redirect myfile" patternSyntax="Wildcard" stopProcessing="true">
<match url="*/myfile.xml" />
<action type="Rewrite" url="/actual/path/myfile.xml" />
</rule>
</rules>
</rewrite>
Upvotes: 1
Reputation: 46008
Do you really have to use routing? How about a good old HttpHandler?
<configuration>
<system.web>
<httpHandlers>
<add verb="GET"
path="*/myfile.xml"
type="MyFileHandler"/>
</httpHandlers>
<system.web>
</configuration>
Upvotes: 1