ASP.NET MVC 路由在虚拟目录中不起作用

发布于 2024-09-10 21:54:35 字数 762 浏览 4 评论 0原文

我有一个 asp.net mvc 2 应用程序(使用 .net 4.0),在虚拟目录中托管时无法正确路由。我有以下简单的路由规则:

        routes.MapRoute(
            "Default", // Route name
            "{action}", // URL with parameters
            new { controller = "accounts" } // Parameter defaults
        );

我正在尝试解析 http://mydomain.com/accounts/new< /a>.其中“accounts”是虚拟目录。如果我将应用程序放在 IIS 网站的根目录中,它可以很好地路由 http://mydomain.com/new,但如果我将应用程序放在虚拟目录中,则会收到 404 错误。我已经调试过,它正在执行 global.asax 并在 vdir 中配置路由。对于虚拟目录中的路由,我需要做一些特殊的事情吗?

供参考。我使用的是 vdir,因为根目录中有 wordpress。

谢谢!

另一件事是,如果我在参数默认值中指定默认操作,它将执行默认操作/控制器,但它永远不会匹配其他任何内容。

I have an asp.net mvc 2 app (using .net 4.0) that isn't routing correctly when hosted in a virtual directory. I have the following simple routing rule:

        routes.MapRoute(
            "Default", // Route name
            "{action}", // URL with parameters
            new { controller = "accounts" } // Parameter defaults
        );

I'm trying to resolve http://mydomain.com/accounts/new. Where "accounts" is a virtual directory. If I put the app in the root of an IIS website it routes fine for http://mydomain.com/new, but if I put the app in a virtual directory I get 404 errors. I've debugged and it is executing global.asax and configuring routing when in the vdir. Is there something special I need to do for routing in a virtual directory?

FYI. I'm using a vdir because the root has wordpress in it.

Thanks!

one more thing is that if I specify a default action in parameter defaults, it will execute the default action/controller, but it never matches anything else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-09-17 21:54:35

我想通了。 Wordpress(我安装在网站根目录下)配置了一些 URL 重写规则,这些规则阻止 asp.net mvc 接收除虚拟目录根目录之外的任何请求。任何超出此路径的内容都会被重写到index.php,这当然在我的mvc应用程序中不存在。

我删除了重写规则,现在一切都按预期进行。

I figured it out. Wordpress (which I had installed at the website root) had configured some URL rewrite rules which were preventing asp.net mvc receiving any requests other than the virtual directory root. Anything with a path beyond that was being rewritten to index.php which of course didn't exist in my mvc app.

I removed the rewrite rule, and now everything works as expected.

草莓味的萝莉 2024-09-17 21:54:35

如果你把它改成:

routes.MapRoute( 
    "Default", // Route name 
    "accounts/{action}", // URL with parameters (BUT WITH ACCOUNTS PREFIX)
    new { controller = "accounts" } // Parameter defaults 
); 

does it work if you change it to:

routes.MapRoute( 
    "Default", // Route name 
    "accounts/{action}", // URL with parameters (BUT WITH ACCOUNTS PREFIX)
    new { controller = "accounts" } // Parameter defaults 
); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文