使用查询字符串参数重写旧版URL

时间:2022-08-24 12:20:28

I've looked at ASP.Net MVC routing legacy URLs passing querystring Ids to controller actions and several other similar posts for legacy URL routing, but I can't get past the error "The RouteData must contain an item named 'controller' with a non-empty string value." Looking this up on line didn't give me any hints to solve my problem.

我已经看过ASP.Net MVC路由遗留URL将querystring Ids传递给控制器​​动作以及其他几个类似的遗留URL路由帖子,但是我无法通过错误“RouteData必须包含一个名为'controller'的项目非空字符串值。“在线查看并没有给我任何解决我的问题的提示。

I've implemented the Legacy routing class described in the link above, and this is what I've defined in the routing table:

我已经实现了上面链接中描述的Legacy路由类,这就是我在路由表中定义的:

        routes.Add(
            "Legacy", 
            new LegacyRoute("fooref.aspx", 
            "FooRef", 
            new LegacyRouteHandler())
        );

        routes.MapRoute(
            "FooRef",
            "{controller}/{action}",
            new
            {
                controller = "Home",
                action = "Index",
                foo_id = UrlParameter.Optional,
                bar_id = UrlParameter.Optional
            }
        );

When I use Phil Haack's route debugger, it indicates that fooref.aspx has a match, but when I turn the route debugger off, I get the error above. If I reverse the statement order, I get "Resource not found" for /ctprefer.aspx, which makes sense -- so it appears to be finding that as a valid route when put in the other order.

当我使用Phil Haack的路由调试器时,它表示fooref.aspx有匹配,但当我关闭路由调试器时,我得到上面的错误。如果我反转语句顺序,我会为/ctprefer.aspx找到“未找到资源”,这是有意义的 - 因此它似乎是在以其他顺序放入时作为有效路由。

Where do I need to declare this missing controller reference?

我在哪里需要声明这个缺少的控制器参考?

Have routing requirements changed for ASP.NET MVC 2 RTM?

ASP.NET MVC 2 RTM的路由要求是否已更改?

2 个解决方案

#1


1  

The solution to this is to use an IHttpHandler directly, rather than an MVCHandler. I have posted code that works with MVC 3 to my blog: http://www.olsonsoft.com/blogs/stefanolson/post/Handling-Legacy-Urls-in-AspNet-MVC-3.aspx

解决方案是直接使用IHttpHandler,而不是MVCHandler。我已经将与MVC 3一起使用的代码发布到我的博客:http://www.olsonsoft.com/blogs/stefanolson/post/Handling-Legacy-Urls-in-AspNet-MVC-3.aspx

...Stefan

...斯特凡

#2


0  

You might want to take a look at URL Rewrite module for IIS. It can be used to translate legacy URL to your new MVC URLs without 'polluting' your app with legacy routes.

您可能需要查看IIS的URL重写模块。它可用于将旧版网址转换为新的MVC网址,而不会使用旧版路由“污染”您的应用。

Don't know if it will fit your solution but it's worth having an alternative.

不知道它是否适合您的解决方案,但值得选择。

#1


1  

The solution to this is to use an IHttpHandler directly, rather than an MVCHandler. I have posted code that works with MVC 3 to my blog: http://www.olsonsoft.com/blogs/stefanolson/post/Handling-Legacy-Urls-in-AspNet-MVC-3.aspx

解决方案是直接使用IHttpHandler,而不是MVCHandler。我已经将与MVC 3一起使用的代码发布到我的博客:http://www.olsonsoft.com/blogs/stefanolson/post/Handling-Legacy-Urls-in-AspNet-MVC-3.aspx

...Stefan

...斯特凡

#2


0  

You might want to take a look at URL Rewrite module for IIS. It can be used to translate legacy URL to your new MVC URLs without 'polluting' your app with legacy routes.

您可能需要查看IIS的URL重写模块。它可用于将旧版网址转换为新的MVC网址,而不会使用旧版路由“污染”您的应用。

Don't know if it will fit your solution but it's worth having an alternative.

不知道它是否适合您的解决方案,但值得选择。