1

I am working on an existing ASP.NET webforms project - slowly updating to MVC. I am trying to conditionally override some of the existing webforms pages by routing to new MVC controllers. I can easily route an "example.aspx" URL to an MVC controller, but it doesn't work if there is an existing "example.aspx" file.

Example code:

routes.MapRoute
            (
                name: "example",
                url: "example.aspx",
                defaults: new
                {
                    controller = "Example",
                    action = "Index"
                }
                constraints: new { useMvc= "1" }
            );

The code above works fine, and routes to the controller as expected, unless there is an existing "example.aspx" file in my solution - in which case it routes to that instead. But that's exactly what I'm trying to override.

Are existing webform routes given preferential treatment? Is there some way to circumvent this?

I've found mechanisms to do the reverse by using MapPageRoute(), but as far as I can see, that doesn't help me.

Scott Hammer
  • 111
  • 3
  • If you're trying to override, why not just delete `example.aspx` once you've added the route? – mason Jul 20 '15 at 13:33
  • This may help http://stackoverflow.com/a/11258217/1663001 – DavidG Jul 20 '15 at 13:40
  • @mason: Because it's conditional based on the constraint. Sometimes I want to override, but sometimes I need the old functionality. – Scott Hammer Jul 20 '15 at 13:44
  • @DavidG: nice find there with that link. Unfortunately, as stated there, the runAllManagedModulesForAllRequests option comes with performance and security issues. Likewise, I'd like to avoid the URLRewriter option since it's not base C#. Surely there's a way in C# to reroute old webform pages (without removing them)? – Scott Hammer Jul 20 '15 at 13:54
  • @ScottHammer Unless you are running a website that is performance critical, I think you don't need to worry about it. – DavidG Jul 20 '15 at 14:48
  • @DavidG: agreed. Unfortunately, the product I'm working on is performance critical. It's running thousands of simultaneous high-security users, with more every day, but it already has speed issues. Thank you for your help. Your link would probably solve this issue for most cases. – Scott Hammer Jul 20 '15 at 16:08
  • You are essentially asking for all requests to run through the ASP.Net pipe, there will be a performance hit if you do that - it's your only choice. – DavidG Jul 20 '15 at 16:34

2 Answers2

0

You can configure your application to ignore specific routes that your are handling through MVC. You could add the following line before registering a new route:

routes.IgnoreRoute("{*path}/example.aspx");

Hope this helps.

Shashank Chaturvedi
  • 2,756
  • 19
  • 29
  • Already tried that. From what I can see, it doesn't matter what's in your routes. If there is an asp webform that matches the URL called, then that automatically overrides the specified routes - whether they are set or whether they are set to be ignored. – Scott Hammer Jul 20 '15 at 16:07
0

I haven't solved the problem as stated, but I've found a work-around that will accomplish the same result.

1) I renamed the "example.aspx" file to something else "foo.aspx"

2) Added the MapRoute() similar to my original question (with the constraint)

3) Used MapPageRoute() to route all other "example.aspx" calls to "foo.aspx"

I also ended up having to use the QueryStringConstraint logic as described by another stackoverflow answer: but that's a separate issue.

I'll leave this question open for a few more days to see if there is a "real" solution before marking my own answer.

Community
  • 1
  • 1
Scott Hammer
  • 111
  • 3
  • Perhaps even better: instead of renaming the original file, you can move it to another directory. For example, a directory named "WebformsRoutedByMVC". The only requirement is that the original built-in routing will no longer find the file based on the URL given. – Scott Hammer Jul 22 '15 at 12:45