jocull
jocull

Reputation: 21095

MVC3 route by requested filename

I have a Flash based component that insists on asking for an XML settings file in whatever "directory" I am currently in.

That means it's asking for paths like...

/Home/mysettings.xml
/Home/Record/mysettings.xml
/AnotherController/AnotherAction/mysettings.xml

This is annoying. I'm trying to create an MVC route to catch anything with mysettings.xml and send them to a static action. I've created a view for the XML and it's possible I'll need to swap the variables on the fly (using URL queries of some kind, probably).

Here is a first stab in my Global.asax.cs:

routes.MapRoute(
    "XmlOverride",
    "mysettings.xml",
    new { controller = "Home", action = "MyXML" }
);

But I'm missing something. It's either 404ing or not going to the right location no matter what I do to it. Is there a way I can get it to route to the right action? Can I preserve the URL vars in the process?

Thanks!

Update

I have a hacked up version working like this. I'm not sure it's the best way to go, but it seems to work. Does anyone have better solutions?

routes.MapRoute(
    "XmlOverride",
    "{c}/mysettings.xml",
    new { controller = "Home", action = "MyXML" }
);
routes.MapRoute(
    "XmlOverride+Action",
    "{c}/{a}/mysettings.xml",
    new { controller = "Home", action = "MyXML" }
);

You can't use {controller} or {action} because it's at a fixed path.

Upvotes: 0

Views: 104

Answers (1)

Jay
Jay

Reputation: 6294

Based on this answer you could rewrite the url.

<rewrite>
  <rules>
    <rule name="MySettingsRewrite">
      <match url="(\w+)mysettings\.xml$" />
      <action type="Rewrite" url="/Home/MyXML" />
    </rule>
  </rules>
</rewrite>

You'll have to double check the regex not sure if I got that right.

The only other option I can think of is to implement your own Route Handler class and use that.

Upvotes: 1

Related Questions